What is the difference between executing php from command line and from Http side .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 Commandline or HTTP side.

+1  A: 

Whether PHP is invoked via a web server module or CLI, the same binary base is used (but can sometimes be configured to use different ini's which can affect the script). It's environment will also be different so environment variables will not be exact.

PHP is also aware that it's been invoked differently and will tailor it's output to suit that (i.e., phpinfo(); output will be formatted differently when called via CLI).

@Wrikken. That must have been done purposefully by the admin for some reason. Either way, I've referred to it now as `binary base`

When you are executing php from command line your server apache or iis have no role to play. You just use php4 or php5 folder to execute your code. There may be differences with the execution as per the difference in libraries available and php.ini settings in the two folders. When running from apache php.ini within your apache/bin is used. When from command line php.ini from your php5 or php4 folder is used.

sushil bharwani
+1  A: 

For the most part, everything is the same. Big differences are that the super globals might not be populated. Best place to look at these would be on php.net http://php.net/manual/en/features.commandline.php

+1  A: 

echo "\r\n" works. (sorry, I couldn't resist)


Main difference is argument passing: running from the CLI, you don't have $_GET, $_POST, $_SESSION, etc; so arguments have to be passed as command line parameters and accessed using if $_SERVER['argc'] and $_SERVER['argv'] Watch out for the directory your code is running in and the include path; and make sure you know what php.ini you've loaded. When outputting, HTML markup doesn't render as markup, but displays as < h1> etc.... watch out particularly for
(PHP_EOL is extremely useful) and multiple spaces or tabs actually appear as multiple spaces or tabs rathe rthan a single space. Forget headers() and other http-specific functions

Mark Baker

Other than what already said, there will likely be differences in privileges as to what parts of the filesystem are accessible: PHP via webserver is run as the webserver user, PHP from command line is run as yourself.

+1  A: 

No html markup in errors
This is a php.ini setting(html_errors), but for the cli this default to off.

Logging to stderr
For HTTP Errors are logged to apache.log, but for in cli the errors are logged to stderr.

The php.ini file that is used for the HttpSide can be a different file than the php.ini for cli. Which can lead to some nasty bugs.

Everything shows as text
var_dump() is readable without a <pre>
No diffence between header('Hello'); and echo('Hello');

Bob Fanger