PHP/CLI: Difference between revisions
from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< PHP
No edit summary |
No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 6: | Line 6: | ||
* <code>[https://www.php.net/manual/en/reserved.variables.argv.php $argv][]</code> contains an array of all the arguments passed to the script | * <code>[https://www.php.net/manual/en/reserved.variables.argv.php $argv][]</code> contains an array of all the arguments passed to the script | ||
** <code>$argv[0]</code> is always the name that was used to run the script. | ** <code>$argv[0]</code> is always the name that was used to run the script. | ||
Note that these variables are only present in the global scope; to use them inside of objects, they will need to be passed in somehow. | |||
Other: | Other: | ||
| Line 12: | Line 14: | ||
* <code>[https://www.php.net/manual/en/function.cli-get-process-title.php cli_get_process_title()]</code> | * <code>[https://www.php.net/manual/en/function.cli-get-process-title.php cli_get_process_title()]</code> | ||
* <code>[https://www.php.net/manual/en/function.cli-set-process-title.php cli_set_process_title()]</code> | * <code>[https://www.php.net/manual/en/function.cli-set-process-title.php cli_set_process_title()]</code> | ||
See also {{l/same|file/glob}} for interfacing with the CLI environment's file-[[globbing]] services | |||
==Interactivity== | ==Interactivity== | ||
* [https://www.php.net/manual/en/book.readline.php GNU Readline] manages editable input lines | * [https://www.php.net/manual/en/book.readline.php GNU Readline] manages editable input lines | ||
* <code>[https://www.php.net/manual/en/function.fgetc.php fgetc()]</code> reads a single character, but waits for a CRLF | * <code>[https://www.php.net/manual/en/function.fgetc.php fgetc()]</code> reads a single character, but waits for a CRLF | ||
** See the comments for ways to wait for the user to actually just press a single character. | ** See the comments for ways to wait for the user to actually just press a single character. | ||
* [https://pecl.php.net/package/ncurses ncurses]: [[PECL]] package last updated in 2012 | |||
** [https://toot.cat/@woozle/108742286886170198 2022-07-31 brief discussion] | |||
==Links== | ==Links== | ||
* [https://www.php.net/manual/en/features.commandline.php Using PHP from the command line] | * [https://www.php.net/manual/en/features.commandline.php Using PHP from the command line] | ||
Latest revision as of 16:34, 29 September 2023
Although PHP is most commonly used to write web applications, it can also be used for scripts that run from a command line interface (CLI). The CLI environment is a little different than the web environment, and PHP's current documentation doesn't make it easy to locate the relevant pieces.
Arguments
Predefined variables:
$argccontains the number of arguments passed to the current script$argv[]contains an array of all the arguments passed to the script$argv[0]is always the name that was used to run the script.
Note that these variables are only present in the global scope; to use them inside of objects, they will need to be passed in somehow.
Other:
getopt()parses individual arguments in a standard way- Note that this ignores any options listed after non-options.
cli_get_process_title()cli_set_process_title()
See also file/glob for interfacing with the CLI environment's file-globbing services
Interactivity
- GNU Readline manages editable input lines
fgetc()reads a single character, but waits for a CRLF- See the comments for ways to wait for the user to actually just press a single character.
- ncurses: PECL package last updated in 2012
