Someone
Someone

Reputation: 10575

Difference between executing PHP code from the commandline and from the HTTP side

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?

Upvotes: 14

Views: 7192

Answers (7)

Bob Fanger
Bob Fanger

Reputation: 29897

No HTML markup in errors

This is a php.ini setting (html_errors), but this defaults to off in the CLI version.

Logging to standard error

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).

php.ini

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.).

Different executables

It's possible to install multiple version of PHP (PHP 8 alongside PHP 7). Use which php to determine which version you're using.

Everything is shown as text

var_dump() is readable without a <pre>. There isn't any difference between header('Hello'); and echo('Hello');.

Upvotes: 7

baao
baao

Reputation: 73251

Extending Bob Fanger's answer a bit:

Running PHP files from the command line is quite trivial. You only have to keep in mind that there are some differences from running the file on a web server or on a servers CLI:

  • No cookies available

  • No $_GET, $_POST, $_SESSION available But you can use $argv to get parameters passed as an argument to the command. The first value is always the file name.

    For example, taken this file:

    <?php
    var_dump($argv);
    ?>
    

called like this:

    user@ubuntu:$ /usr/bin/php /home/user/file.php foo bar

would give you this output:

    array(4) {
      [0]=>
      string(8) "file.php"
      [1]=>
      string(3) "foo"
      [2]=>
      string(3) "bar"
    }
  • Full file paths from your servers root are required You will need to provide the full paths to your files (e.g., for include(), require(), file_get_contents(), ... ), even though they might be in the same folder.

  • different user/permission settings. The files aren't getting executed by www-data user, but by the user you are logged in into your machine. This affects all your file's function calls that effect the machines file system, (e.g. mkdir(), include(), ... ), so you have to make sure to give appropriate permission to that user.

Upvotes: 0

Matteo Riva
Matteo Riva

Reputation: 25060

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

Upvotes: 1

Mark Baker
Mark Baker

Reputation: 212422

The 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 file you've loaded.

When outputting, HTML markup doesn't render as markup, but displays as <h1>, etc.... watch out particularly for <br /> (PHP_EOL is extremely useful) and multiple spaces or tabs actually appear as multiple spaces or tabs rather than a single space.

Forget headers() and other HTTP-specific functions.

Upvotes: 2

MANCHUCK
MANCHUCK

Reputation: 2472

For the most part, everything is the same. The big differences are that the super globals might not be populated.

The best place to look at these would be on php.net, Using PHP from the command line

Upvotes: 1

sushil bharwani
sushil bharwani

Reputation: 30187

When you are executing PHP code from the command line, your server, Apache or IIS, has no role to play.

You just use the PHP 4 or PHP 5 folder to execute your code. There may be differences with the execution as per the difference in libraries available and file php.ini settings in the two folders. When running from Apache, file php.ini within your apache/bin folder is used.

When from the command line, file php.ini from your php5 or php4 folder is used.

Upvotes: 1

webbiedave
webbiedave

Reputation: 48897

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). Its 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 its output to suit that (i.e., phpinfo(); output will be formatted differently when called via CLI).

Upvotes: 2

Related Questions