What is the difference between executing PHP code from the command line and from HTTP?
Do they use the same executable such as (php.exe or php-cgi.exe (Apache or IIS))? Do the results differ when they are executing from the command line or HTTP?
There are two variables you can use while writing command line applications with PHP: $argc and $argv. The first is the number of arguments plus one (the name of the script running). The second is an array containing the arguments, starting with the script name as number zero ($argv[0]).
The CLI SAPI differs from other PHP SAPIs and these differences are documented in PHP Command Line Usage: Differences to other SAPIs. PHP is not just for web pages and web sites anymore. It can be used for command line scripting as well.
The CLI SAPI provides an interactive shell using the -a option if PHP is compiled with the --with-readline option. As of PHP 7.1. 0 the interactive shell is also available on Windows, if the readline extension is enabled. Using the interactive shell you are able to type PHP code and have it executed directly.
This is a php.ini setting (html_errors), but this defaults to off in the CLI version.
Usually errors are logged to the web server's error.log file, but in the CLI version, errors are written to standard error.
This is also available as a php.ini setting (error_log).
The php.ini file that is used for the CLI version can be a different file. Which can lead to some nasty bugs (curl suddenly not available, etc.).
It's possible to install multiple version of PHP (PHP 8 alongside PHP 7). Use which php
to determine which version you're using.
var_dump() is readable without a <pre>
. There isn't any
difference between header('Hello');
and echo('Hello');
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With