PHP CLI Posted by: Jordan in ProgrammingPHP on
Since  PHP 4.3.0 the CLI (Command Line Interface) version of PHP is automatically installed by default when PHP is installed.  Shell script was possible using CGI PHP prior to 4.3 and since 3.0 but it wasn't suited for the job.  CLI PHP was built for shell scripting and has none of the webserver overhead (such as GET/POST variables, Output variables and Headers). 

Executing PHP from the shell (command prompt for Windows Users) you are automatically using PHP CLI.   You can make sure by executing this command:

# php -v

Which will output:

PHP 5.2.6 (cli) (built: Jun 4 2008 11:44:40)
Copyright (c) 1997-2008 The PHP Group
Zend
Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
with the ionCube
PHP Loader v3.1.32, Copyright (c) 2002-2007, by ionCube Ltd., and
with Zend
Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend Technologies
with Suhosin
v0.9.23, Copyright (c) 2007, by SektionEins GmbH
with Zend
Optimizer v3.3.3, Copyright (c) 1998-2007, by Zend Technologies


Notice the bold cli above to the right of the version?  This means you are using PHP CLI.


Using the hash-bang
The hash-bang or shebang is the first line of most shell scripts.  For example, here is the first line of a bash script and a perl script:

#!/bin/sh
#!/usr/bin/perl


These lines in executable files tell the OS to execute the rest of the file using the specified program (sh for bash, perl for perl).  PHP CLI can now use the hash-bang:

#!/usr/bin/php

Which will allow you to execute the PHP script like any console script (without having to call the PHP executable first):

# ./myscript.php


Execute Code From the Console

php -r 'print "hello";' 

Don't forget to include the ending ;


Custom php.ini file
PHP CLI has it's own configuration file.  Like most UNIX configuration files the CLI configuration file is found in /etc.   By default PHP CLI looks for /etc/php-cli.ini before using the default /usr/local/lib/php.ini file (in the event php-cli.ini does not exist).  You can use different settings in the CLI configuration file like setting max_execution_time to 0 (no limit). 


Accepting User Input

 Using the STDIN stream you can accept user input:

#!/usr/bin/php -Cq
<? php

$stdin = fopen('php://stdin', 'r');

 echo "Hello, what is your name? ";
$name = trim(fgets($stdin));
echo "Welcome " . $name . "n";

fclose($stdin);
?>

Notice the -Cq in the shebang?  This is because I have no /etc/php-cli.ini file so headers are not suppressed.  The "C" causes PHP to not change its working directory to the main script (/usr/bin/). The "q" suppresses HTTP headers in PHP output.


Command Line Options
What would a shell script be without letting the user specify run-time arguments?  Just like most programming languages these are store in an array named "$argv".  $argv[0] will always be the executing script name.

foreach ($argv as $opt) {
print "$optn";
}

 

Forking
Forking is a UNIX term similar to threading (or creating a thread) in Windows.   When you create a FORK you copy the entire process that the fork is initiated from.  This makes forking impractical if you are not using PHP in CLI mode because it will copy all of the Apache/IIS/HTTP Server memory, files, settings, etc. that can cause an HTTP server to crash.

In PHP CLI none of the HTTP environment is available which makes forking more realistic.  Forking is also used to daemonize a PHP script.  This term means create a "background process" where the PHP script detaches itself from the terminal. 

You can read more about forking here.


Need Help?
Ask in our forum!







Trackback(0)
feed0 Comments

Write comment
 
 
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger
 

security image
Write the displayed characters


busy