Always wanted to, at the command line can not run the PHP program, like C language can get parameters through the command line. Today, try to find the feeling is very interesting, usually write the program to play when you can use this, let me take a look at how to do. My environment is php5.6.
A, PHP CLI mode what is CLI? Is the CLI SAPI called CLI (Command line Interface) that PHP has provided since version 4.3, and is mainly used in the development of PHP shell applications. The bottom line is to support the command-line parsing of PHP code. The shell application can be understood as a parser that provides a command-line interface. such as the shell under Linux and so on. From PHP5, the CLI's executable file in the PHP release package is php.exe,cgi version php-cgi.exe, and includes a Php-win.exe file that is equivalent to the CLI version and does not output any information. In fact, the configuration window under the environment variables are mainly these several files.
Second, under the command to execute PHP program to run the PHP program at the command line, you can not need to set window, directly execute similar commands like the following:
The first half of the F:\XAMPP\php\php.exe-f "F:\XAMPP\htdocs\test.php"---Arg1-arg2 is where Php.exe is located, and-F is the path to execute the php file after the option-ARG1-ARG2 is the With the parameters. It's a hassle to do this every time, so check the relevant information and find another way to feel this method is very convenient, let's take a look at the following.
We only need to do the following steps: 1. Add Php.exe, Php-win.exe or Php-cli.exe path 2 to the environment variable, adding the. php suffix to Pathtext
3. Associate the. php suffix into a file type, executed with the following command
Assoc. Php=phpfile 4, to associate a phpfile type of file with a related executable program, execute with the following command
Ftype phpfile= "F:\XAMPP\PHP\php.exe"-F "%1"-%~2 so that it can be executed directly without php.exe the path of the file and the suffix of the PHP file. This may encounter the antivirus software to modify the registry prompts, directly all allow the good.
There may be questions, so how do we get the arguments in the command line in the PHP code, in fact the arguments can be in $_server: Enter at the command line:
demo_cli.php 123456789 The meaning of this order is to execute demo_cli. The PHP parameter is 123456789; next we'll print the output to see what's $_server (only part of the show):
' argv ' => Array (2) {[0] => string ($) F:\XAMPP\htdocs\ [1] => string (9) "123456789"} ' argc ' => int (2) can see a RGV has the value of the parameter we want, directly take it.
Three, in fact PHP has a lot of interesting things, need to find, PHP is very interesting. Reference: http://php.net/manual/zh/install.windows.commandline.php