Teb*_*ebe 3 php command-line-interface
我正在阅读 CLI 模式下 PHP 的细节,但我无法为自己解释什么实用程序具有 -f 标志。
可以将任何 php 脚本作为"php name_of_script.php" 或"php -f name_of_script.php"
我想这个选项只是一种语法糖。此外,它的存在也许可以通过以下事实来解释:当用户看到 -f 该文件被执行时,它对用户来说更加明显。我无法做出任何其他解释。有人看到它的任何其他用法吗?
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
...
Run Code Online (Sandbox Code Playgroud)
However, with -f, it'll work:
$ php -f -.php
<script executes successfully>
Run Code Online (Sandbox Code Playgroud)