Behavioural differences on the command line
suggest changeWhen running from the CLI, PHP exhibits some different behaviours than when run from a web server. These differences should be kept in mind, especially in the case where the same script might be run from both environments.
- No directory change. Some server SAPIs set the current working directory to the directory of the script. The CLI SAPI does not: the working directory stays wherever you were when you started PHP.
require("./stuff.inc")is therefore relative to that directory, not to the script. Scripts run from the command line should use absolute paths or the__DIR__constant.__DIR__and__FILE__still refer to the script file. - No output buffering The
php.inidirectivesoutput_bufferingandimplicit_flushdefault tofalseandtrue, respectively. Buffering is still available, but must be explicitly enabled, otherwise output will always be displayed in real time. - No time limit The
php.inidirectivemax_execution_timeis set to zero, so scripts will not time out by default. - No HTML errors In the event you have enabled the
php.inidirectivehtml_errors, it will be ignored on the command line. - A different
php.inimay be loaded. CLI PHP can use a differentphp.inifrom the web server. Runphp --inito see which file is in use.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents