Hoofdstuk 23. Using PHP from the command line

Opmerking :
















  1. 
    
    
    
    Both ways (whether using the -f switch or not) execute the 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 have any name or extension you wish.

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

    Opmerking :

  3. 
    
    
    


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







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



As you see, in this case no care needs to be taken when passing parameters which start with - to your script.

Tabel 23-3 .



Opmerking :

Opmerking :





















Opmerking :






Opmerking :








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



The correct way would be to use single quotes ' . Variables in single-quoted strings 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 can still easily run into troubles when trying to get shell variables into the code or using backslashes for escaping. You've been warned.

Opmerking: -r is available in the

Voorbeeld 23-1 .
















Voorbeeld 23-2 .