How to use PHP code in Linux command line?

You can use PHP code thanks to the CLI SAPI (command-line interpreter/interface Server Application Programming Interface). It can serve you for developing of shell application with PHP, straight from the command line of your Linux computer. 

You can use PHP code in the Linux command line in 3 different ways.

1. Use PHP to execute a specific file.

Let’s say we have a PHP script that is called my_script.php. We can execute it with the following command: 

$ php my_script.php

or 

$ php -f my_script.php

You can use “php” or the “-f” switch. Having the php extension at the end is not required. 

2. Directly pass the PHP code to execute on the command line. 

$ php -r ‘print_r(get_defined_constants());’

You need to be especially careful with shell variable substitution and usage of quotes. In this case, we are using the “-r” switch that does not need tags for beginning or ending. If you add those tags, that will lead to an error. 

3. Execute PHP code via standard input (stdin). 

You can create PHP code dynamically and put it in the binary. See the example below: 

$ some_application | some_filter | php | sort -u > final_output.txt

The PHP binary accepts different arguments, plus the PHP script can receive more arguments. The arguments are not limited by PHP. The arguments are available in the global array $argv (Array of arguments passed to script). The first index should contain the script’s name and the way you are calling it from the command line. If you are using the second (-r) or the third way (stdin), the value of $argv[0] will be “-“. 

$argc, a second global variable, has the number of elements in the array $argv. 

In case the arguments passed to the script do not start with “-“, there is nothing extra to think about. If the character exists, the interpreter will try to handle it, even before the script. You can evade troubles with list separator “–”

# This will not execute the given code but will show the PHP usage

$ php -r ‘var_dump($argv);’ -h

Usage: php [options] [-f] <file> [args…]

[…]

# This will pass the ‘-h’ argument to the script and prevent PHP from showing its usage

$ php -r ‘var_dump($argv);’ — -h

array(2) {

  [0]=>

  string(1) “-“

  [1]=>

  string(2) “-h”

}

Use PHP code on Unix-based system.

Here you will need to take care of one important thing – add the path to the PHP CLI with “#” sign before – #!/usr/bin/php

 Apart from this, everything should work fine. 

Example of PHP script executes as a shell script

#!/usr/bin/php

 <?php

 var_dump($argv);

 ?> 

We asume we are at the current directory and we have a test file: 

$ chmod +x test

$ ./test -h — foo

array(4) {

  [0]=>

  string(6) “./test”

  [1]=>

  string(2) “-h”

  [2]=>

  string(2) “–“

  [3]=>

  string(3) “foo”

}

No worries about the “-” sign. 

In the beginning, the special “#!” shows to the system which program it must use to run it. 

On Windows, you can associate double-click on a .php extension with the php.exe. If the script starts with “#!” it does not bother Windows users because it just takes it as a comment. 

Example of script (script.php), ment to be run from the command line:

#!/usr/bin/php

 <?php

 if ($argc != 2 || in_array($argv[1], array(‘–help’, ‘-help’, ‘-h’, ‘-?’))) {

 ?>

 This is a command-line PHP script with one option.

   Usage:

   <?php echo $argv[0]; ?> <option>

   <option> can be some word you would like

   to print out. With the –help, -help, -h,

   or -? options, you can get this help.

 <?php

 } else {

    echo $argv[1];

 }

 ?>

The program will first check for a required argument (after checking the script name). If there is no argument or there is “–help”, “-help”, “-h”, or “-?” it will print the help message. 

Get IPv4 address with PHP explained

Leave a Reply

Your email address will not be published. Required fields are marked *