<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Differences to other SAPIs</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.commandline.introduction.html">Introduction</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.commandline.options.html">Options</a></div> <div class="up"><a href="features.commandline.html">Command line usage</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="features.commandline.differences" class="section"> <h2 class="title">Differences to other <acronym title="Server Application Programming Interface">SAPI</acronym>s</h2> <p class="para"> Remarkable differences of the <acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> compared to other <acronym title="Server Application Programming Interface">SAPI</acronym>s: <ul class="itemizedlist"> <li class="listitem"> <p class="para"> Unlike the <acronym title="Common Gateway Interface">CGI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym>, no headers are written to the output. </p> <p class="para"> Though the <acronym title="Common Gateway Interface">CGI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> provides a way to suppress HTTP headers, there's no equivalent switch to enable them in the <acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym>. </p> <p class="para"> <acronym title="Command Line Interpreter/Interface">CLI</acronym> is started up in quiet mode by default, though the <strong class="option unknown">-q</strong> and <strong class="option unknown">--no-header</strong> switches are kept for compatibility so that it is possible to use older <acronym title="Common Gateway Interface">CGI</acronym> scripts. </p> <p class="para"> It does not change the working directory to that of the script. (<strong class="option unknown">-C</strong> and <strong class="option unknown">--no-chdir</strong> switches kept for compatibility) </p> <p class="para"> Plain text error messages (no <acronym title="Hyper Text Markup Language">HTML</acronym> formatting). </p> </li> <li class="listitem"> <p class="para"> There are certain <var class="filename">php.ini</var> directives which are overridden by the <acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> because they do not make sense in shell environments: </p> <p class="para"> <table class="doctable table"> <caption><strong>Overridden <var class="filename">php.ini</var> directives</strong></caption> <thead> <tr> <th>Directive</th> <th><acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> default value</th> <th>Comment</th> </tr> </thead> <tbody class="tbody"> <tr> <td><a href="errorfunc.configuration.html#ini.html-errors" class="link">html_errors</a></td> <td><strong><code>FALSE</code></strong></td> <td> Defaults to <strong><code>FALSE</code></strong>, as it can be quite hard to read error messages in the shell enviroment when they are cluttered up with uninterpreted <acronym title="Hyper Text Markup Language">HTML</acronym> tags. </td> </tr> <tr> <td><a href="outcontrol.configuration.html#ini.implicit-flush" class="link">implicit_flush</a></td> <td><strong><code>TRUE</code></strong></td> <td> In a shell environment, it is usually desirable for output, such as from <span class="function"><a href="function.print.html" class="function">print</a></span>, <span class="function"><a href="function.echo.html" class="function">echo</a></span> and friends, to be displayed immediately, and not held in a buffer. Nonetheless, it is still possible to use <a href="ref.outcontrol.html" class="link">output buffering</a> to defer or manipulate standard output. </td> </tr> <tr> <td><a href="info.configuration.html#ini.max-execution-time" class="link">max_execution_time</a></td> <td>0 (unlimited)</td> <td> PHP in a shell environment tends to be used for a much more diverse range of purposes than typical Web-based scripts, and as these can be very long-running, the maximum execution time is set to unlimited. </td> </tr> <tr> <td><a href="ini.core.html#ini.register-argc-argv" class="link">register_argc_argv</a></td> <td><strong><code>TRUE</code></strong></td> <td> <p class="para"> Setting this to <strong><code>TRUE</code></strong> means that scripts executed via the <acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> always have access to <em class="emphasis">argc</em> (number of arguments passed to the application) and <em class="emphasis">argv</em> (array of the actual arguments). </p> <p class="para"> The PHP variables <var class="varname"><var class="varname"><a href="reserved.variables.argc.html" class="classname">$argc</a></var></var> and <var class="varname"><var class="varname"><a href="reserved.variables.argv.html" class="classname">$argv</a></var></var> are automatically set to the appropriate values when using the <acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym>. These values can also be found in the <var class="varname"><var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER</a></var></var> array, for example: <var class="varname"><var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['argv']</a></var></var>. </p> </td> </tr> <tr> <td><a href="outcontrol.configuration.html#ini.output-buffering" class="link">output_buffering</a></td> <td><strong><code>FALSE</code></strong></td> <td> <p class="para"> Although the <var class="filename">php.ini</var> setting is hardcoded to <strong><code>FALSE</code></strong>, the <a href="book.outcontrol.html" class="link">Output buffering</a> functions are available. </p> </td> </tr> <tr> <td><a href="info.configuration.html#ini.max-input-time" class="link">max_input_time</a></td> <td><strong><code>FALSE</code></strong></td> <td> <p class="para"> The PHP <acronym title="Command Line Interpreter/Interface">CLI</acronym> does not support GET, POST or file uploads. </p> </td> </tr> </tbody> </table> </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> These directives cannot be initialized with another value from the configuration file <var class="filename">php.ini</var> or a custom one (if specified). This limitation is because the values are applied after all configuration files have been parsed. However, their values can be changed during runtime (although this is not sensible for all of them, such as <a href="ini.core.html#ini.register-argc-argv" class="link">register_argc_argv</a>). </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> It is recommended to set <a href="misc.configuration.html#ini.ignore-user-abort" class="link">ignore_user_abort</a> for command line scripts. See <span class="function"><a href="function.ignore-user-abort.html" class="function">ignore_user_abort()</a></span> for more information. </p> </p></blockquote> </li> <li class="listitem"> <p class="para"> To ease working in the shell environment, a number of constants are defined for <a href="features.commandline.io-streams.html" class="link">I/O streams </a>. </p> </li> <li class="listitem"> <p class="para"> The <acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> does <em class="emphasis">not</em> change the current directory to the directory of the executed script. </p> <div class="example" id="example-363"> <p><strong>Example #1 Example showing the difference to the <acronym title="Common Gateway Interface">CGI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym>: </strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Our simple test application named test.php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">getcwd</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p> When using the <acronym title="Common Gateway Interface">CGI</acronym> version, the output is: </p></div> <div class="example-contents screen"> <div class="cdata"><pre> $ pwd /tmp $ php -q another_directory/test.php /tmp/another_directory </pre></div> </div> <div class="example-contents"><p> This clearly shows that PHP changes its current directory to the one of the executed script. </p></div> <div class="example-contents"><p> Using the <acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> yields: </p></div> <div class="example-contents screen"> <div class="cdata"><pre> $ pwd /tmp $ php -f another_directory/test.php /tmp </pre></div> </div> <div class="example-contents"><p> This allows greater flexibility when writing shell tools in PHP. </p></div> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The <acronym title="Common Gateway Interface">CGI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> supports this <acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> behaviour by means of the <strong class="option unknown">-C</strong> switch when run from the command line. </p> </p></blockquote> </li> </ul> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.commandline.introduction.html">Introduction</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.commandline.options.html">Options</a></div> <div class="up"><a href="features.commandline.html">Command line usage</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>