פרק 24. Using PHP from the command line

Since version 4.3 , PHP supports a new SAPI type ( Server Application Programming Interface ) named CLI which means Command Line Interface . There are quite some differences between the CLI SAPI and other SAPI s which are further explained throughout this chapter .

The CLI SAPI was released for the first time with PHP 4.2.0 , but was still experimental back then and had to be explicitely enabled with --enable-cli when running . / configure . Since PHP 4.3.0 the CLI SAPI is no longer experimental and is therefore always built and installed as the php ( called php.exe on Windows ) binary .



The list of command line options provided by the PHP binary can be queryied anytime by running PHP with the -h switch :














  1. 
    
    
    
    Both ways (using the -f switch or not) execute the given file my_script.php . You can choose any file to execute, your PHP scripts do not have to end with the .php extension but can give them any name or extension you want them to have.

  2. 
    
    
    
    Special care has to be taken in regards of shell variable substitution and quoting usage.

    הערה : Read the example carefully , thera are no beginning or ending tags !

  3. 
    
    
    


You cannot combine any of the three ways to execute code.

Like every shell application not only the PHP binary accepts a number of arguments but also your PHP script can receive them . The number of arguments which can be passed to your script is not limited by PHP ( the shell has a certain size limit in numbers of characters which can be passed ; usually you won't hit this limit ) .

To prevent this use the argument list separator - - . After the argument has been parsed by PHP , every argument following it is passed untoched / unparsed to your script .




You can write a script which ' s first line starts with # ! / usr / bin / php and then following the normal PHP code included within the PHP starting and end tags and set the execution attributes of the file appropriately . This way it can be executed like a normal shell or perl script :

 
#

 
!

 
/

 
usr

 
/

 
bin

 
/

 
php

 
 ?

 
var_dump(

 
$argv)

 
;

 
?



Assuming this file is named test in the current directory, we can now do the following:



As you see no care has to be taken when passing parameters to your script which start with - .

טבלה 24-2 .

הערה :

הערה :

 
$

 
php

 
-v

 
PHP

 
4.3.0-dev

 
(

 
cli)

 
,

 
Copyright

 
(c

 
)

 
1997-2002

 
The

 
PHP

 
Group

 
Zend

 
Engine

 
v1.2.1

 
,

 
Copyright

 
(c

 
)

 
1998-2002

 
Zend

 
Technologies










This option allows to set a custom value for any of the configuration directives allowed in php.ini .






 
#

 
Ommiting

 
the

 
value

 
part

 
will

 
set

 
the

 
given

 
configuration

 
directive

 
to

 
"

 
1

 
"

 
$

 
php

 
-d

 
max_execution_time

 
-r

 
'$foo

 
=

 
ini_get("max_execution_time")

 
;

 
var_dump($foo)

 
;

 
'

 
string(1

 
)

 
"1

 
"

 
#

 
Passing

 
an

 
empty

 
value

 
part

 
will

 
set

 
the

 
configuration

 
directive

 
to

 
"

 
"

 
php

 
-d

 
max_execution_time=

 
-r

 
'$foo

 
=

 
ini_get("max_execution_time")

 
;

 
var_dump($foo)

 
;

 
'

 
string(0

 
)

 
"

 
"

 
#

 
The

 
configuration

 
directive

 
will

 
be

 
set

 
to

 
anything

 
passed

 
after

 
the

 
'=

 
'

 
character

 
$

 
php

 
-d

 
max_execution_time=20

 
-r

 
'$foo

 
=

 
ini_get("max_execution_time")

 
;

 
var_dump($foo)

 
;

 
'

 
string(2

 
)

 
"20

 
"

 
$

 
php

 
-d

 
max_execution_time=doesntmakesense

 
-r

 
'$foo

 
=

 
ini_get("max_execution_time")

 
;

 
var_dump($foo)

 
;

 
'

 
string(15

 
)

 
"doesntmakesense

 
"





הערה :






If PHP is not working well , it is advisable to make a php -i and see if any error messages are printed out before or in place of the information tables .

The PHP start and end tags ( ?php and ? ) are not needed and will cause a parser errors .

הערה :








The problem here is that the sh/bash performs variable substritution even when using double quotes " . Since the variable $foo is unlikely to be defined, it expands to nothing which results in being the code passed to PHP for executin in fact reads:



The correct way would be to use single quotes ' . variables in strings quoted with single quotes are not expanded by sh/bash.



If you are using a shell different from sh/bash, you might experience further issues. Feel free to open a bug report or send a mail to phpdoc@lists.php.net. One still can easily run intro troubles when trying to get shell variables into the code or using backslashes for escaping. You've been warned.



דוגמה 24-1 .
















In the script above , we used the special first line to indicate , that this file should be run by PHP .

דוגמה 24-2 .

 
@c:\php\php.exe

 
script.php

 
%1

 
%2

 
%3

 
%4



Assuming , you named the above program as script.php , and you have your php.exe in c:\php\php.exe this batch file will run it for you with your added options : script.bat echothis or script.bat -h .