Difference between revisions of "PHP/CLI"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< PHP
Jump to navigation Jump to search
(Created page with "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...")
 
(more stuff that's relevant)
Line 1: Line 1:
 
Although [[PHP]] is most commonly used to write [[web application]]s, 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.
 
Although [[PHP]] is most commonly used to write [[web application]]s, 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.
  
==Predefined Variables==
+
==Functionality==
 +
Predefined variables:
 
* <code>[https://www.php.net/manual/en/reserved.variables.argc.php $argc]</code> contains the number of arguments passed to the current script
 
* <code>[https://www.php.net/manual/en/reserved.variables.argc.php $argc]</code> contains the number of arguments passed to the current 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>[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.
 +
 +
Other:
 +
* <code>[https://www.php.net/manual/en/function.getopt.php getopt()]</code> parses individual arguments in a standard way
 +
* <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.glob.php glob()]</code> is the interface to the system's file-[[globbing]] function
 
==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]

Revision as of 20:39, 5 February 2020

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.

Functionality

Predefined variables:

  • $argc contains 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.

Other:

Links