Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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?

like image 719
Someone Avatar asked Jun 10 '10 16:06

Someone


People also ask

Can PHP be used for command line scripts?

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

Can we use PHP to write command line scripts Yes?

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.

How do I run the interactive PHP shell from the command line interface?

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.


1 Answers

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

like image 67
Bob Fanger Avatar answered Oct 21 '22 11:10

Bob Fanger