From 263d831c26664a2b11f3b21538b588b3786f7d03 Mon Sep 17 00:00:00 2001 From: Hannes Magnusson Date: Mon, 5 Feb 2007 20:19:25 +0000 Subject: [PATCH] Improve markup git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@229064 c90b9560-bf6c-de11-be94-00142212c4b1 --- features/commandline.xml | 75 ++++++++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 18 deletions(-) diff --git a/features/commandline.xml b/features/commandline.xml index d6f476a805..ba3831c4c7 100644 --- a/features/commandline.xml +++ b/features/commandline.xml @@ -1,5 +1,5 @@ - + Using PHP from the command line @@ -294,8 +294,10 @@ php -r 'fwrite(STDERR, "stderr\n");' role="strong">not change the current directory to the directory of the executed script! + Example showing the difference to the CGI SAPI: + ]]> - When using the CGI version, the output is: + + This clearly shows that PHP changes its current directory to the one of the executed script. Using the CLI SAPI yields: + - This allows greater flexibility when writing shell tools in - PHP. + + This allows greater flexibility when writing shell tools in PHP. + The CGI SAPI supports this CLI SAPI @@ -390,7 +395,7 @@ Usage: php [options] [-f] [--] [args...] Telling PHP to execute a certain file. - + + + Both ways (whether using the 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 @@ -410,12 +417,14 @@ php -f my_script.php Pass the PHP code to execute directly on the command line. - + + + Special care has to be taken in regards of shell variable substitution and quoting usage. @@ -436,12 +445,14 @@ php -r 'print_r(get_defined_constants());' This gives the powerful ability to dynamically create PHP code and feed it to the binary, as shown in this (fictional) example: + + final_output.txt ]]> - + You cannot combine any of the three ways to execute code. @@ -471,7 +482,7 @@ $ some_application | some_filter | php | sort -u >final_output.txt PHP, every argument following it is passed untouched to your script. - + - + However, there's another way of using PHP for shell scripting. You can write a script where the first line starts with @@ -498,6 +509,9 @@ array(2) { starting and end tags. Once you have set the execution attributes of the file appropriately (e.g. chmod +x test) your script can be executed like a normal shell or perl script: + + + Execute PHP script as shell script ]]> + 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. @@ -569,9 +587,11 @@ array(4) { --php-ini - With this option one can either specify a directory where to look for - &php.ini; or you can specify a custom INI file - directly (which does not need to be named &php.ini;), e.g.: + This option can either specify a directory where to look for + &php.ini; or specify a custom INI file + (which does not need to be named &php.ini;), e.g.: + + + + If you don't specify this option, file is searched in default locations. @@ -606,8 +628,10 @@ $ php -c /custom/directory/custom-file.ini my_script.php ]]> + Examples (lines are wrapped for layout reasons): + - + @@ -704,9 +728,11 @@ string(15) "doesntmakesense" -m --modules + Using this option, PHP prints out the built in (and loaded) PHP and Zend modules: + - + @@ -745,27 +771,35 @@ ctype to not collide with command line variable substitution done by the shell. + Example showing a parser error + + + 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 at &url.php.bugs;. @@ -848,17 +884,18 @@ array(370) { PHP code to execute after processing the input. Added in PHP 5. - - Example of using , and + + Using the <option>-B</option>, <option>-R</option> and <option>-E</option> options to count the number of lines of a project. + - + @@ -887,8 +924,10 @@ Total Lines: 37328 -v --version + Writes the PHP, PHP SAPI, and Zend version to standard output, e.g. + - +