Tebe
Tebe

Reputation: 3214

php CLI mode -f flag

I was reading about specifics of PHP in CLI mode and I can't explain for myself what utility has -f flag.

It's possible execute any php script as "php name_of_script.php" or "php -f name_of_script.php"

I guess this option is just kind of syntactic sugar. Also its existence can be perhaps explained by the fact that it's more obvious for user when he sees -f that file is executed. I can't make up any other explanations. Do someone see any other usage of it?

Upvotes: 3

Views: 1269

Answers (1)

Matt Gibson
Matt Gibson

Reputation: 38238

PHP has a very long history with a lot of the design decisions lost in the mists of time; I'm not sure anyone will be able to tell you for certain why there's both a -f option and the ability to run a file without any options at all.

However, it certainly seems designed for user convenience; most command line users would expect an interpreter to interpret a filename provided as a single parameter, and it's the most common use-case, so making it the quickest to type makes sense. My guess would be that the PHP CLI started off with just the -f option, and the option to run a file by providing just the filename was added later to make people's lives easier. The -f was retained for backwards compatibility.

I can think of one case where the -f option is useful: if the filename starts with a hyphen, for example -.php.

When provided as a single parameter, this will be treated as an option, and fail:

$ php -.php

Usage: php [options] [-f] <file> [--] [args...]
   php [options] -r <code> [--] [args...]
   php [options] [-B <begin_code>] -R <code
...

However, with -f, it'll work:

$ php -f -.php
<script executes successfully>

Upvotes: 4

Related Questions