PHP command line arguments
I have Linux been installed on my system. On that i use PHP and also had used the command-line interface, let say for example "# php crap.php". Now, i wanted to know that is it possible for me to pass php command-line arguments to a php script? Does any body knows about it? Let me know your views on the above issue. Any help you can provide would be greatly appreciated.
Re: PHP command line arguments
PHP has the getopt() function for getting options or command line arguments from a CLI PHP script. PHP Command Line Interface. As the name implies, this is a way of using PHP in the system command line. This provides a simple way to get values from the command line like e.g. "-a foo".
Re: PHP command line arguments
The command itself (i.e. your script filename) and the command line arguments are stored in an array in the $_SERVER variables called 'argv'. If you have standard installation of PHP for Apache web server, then there are very high chances that you already have PHP CLI installed on your system.
Re: PHP command line arguments
$argv is "argument value". It is possible to access the command line arguments from here but it's easier to getopt() because you can then access the arguments by flag and in any order. Running the command "php myscript.php foo bar" and then doing print_r($_SERVER['argv']) would output this: It's an array that contains an argument (as a string) in each array key, with the name of the script being in $argv[0] and the arguments passed being held after that.
Example:-
Quote:
"# php crap.php crap rocks"
$argv[0] = "crap.php"
$argv[1] = "crap"
$argv[2] = "rocks"