Time for the second episode of my series on command line PHP. In the previous episode, I’ve discussed the basic usage of the technology. Now it’s time turn things up a notch by adding a few options to our PHP binary.
In the command line features chapter of the PHP manual you can get list of all supported CLI options. Please do take a look at it. In this article I will only cover a few options and here’s a list:
- Interactive mode: -a
- Run code: -r
- Define configuration: -d
- Lint check: -l
- List modules: -m
- Configuration info: -l
- Syntax highlighting: -s
- Version info: -v
- Function reflection: –rf
- Class reflection: –rc
- Configuration information: -i
Interactive mode (-a)
When developing PHP code, it is the de facto standard to store your code in a file which will then be interpreted and executed by the PHP binary. The interactive mode allows you the skip the storage aspect and lets you type PHP code on a command line. By pressing the return key, the interactive shell interpretes and executes your code directly.
Notice: your PHP installation needs to be compiled with the readline option !
Example
$ php -a $ php > echo "Give me some interactive output"; Give me some interactive output $ php >
Run code (-r)
Instead of going in interactive mode and typing your PHP code, you can also pass it as a command line argument. This could be a nice to have feature if you need automated code execution or if you want to interact with other binaries via piping.
Notice: be careful with quotes. As you pass your PHP code to the -r argument, you will surround it with quotes. If you would use those same quotes in the script itself, you’ll need to escape them to avoid problems!
Example
$ php -r “echo "Give me some output";” Give me some output $
Define configuration (-d)
As the story goes for configurable Linux software, settings can be overrided at runtime or passed when starting the sofware. Each time the sofware is restarted, those custom settings are lost. So it is no news to you when I state that there’s a need for persistence and that’s where your configuration file comes into play. As all (or at least most) of you know, the php.ini file takes care of that in the wonderful world of PHP.
Sometimes you want to override a settings that was defined without changing the ini file. Using the ini_set() syntax you can do this is in your source code, but when you don’t even want to touch your source, you can use the -d option. The ”-d’ option tells the php binary the value of that argument will contain settings which will be overrided.
In the example below I combine the run code feature with the dynamic configuration feature
Example
$ php -d max_execution_time=20 -r 'echo ini_get("max_execution_time");'
20
$Lint check (-l)
The lint check is a syntax checking utility that scans the input file for syntax errors. It’s not just a manual feature, because you can automate it: I’ve seen it being used as a Subversion pre-commit hook.
Notice: the lint check can’t be combined with the run code (-r) option
It is important to know what the lint check does and more importantly, what it doesn’t:
- It checks if the PHP code in the executed file corresponds to the PHP syntax standards.
- It doesn’t throw any fatal errors (e.g. if a function doesn’t exist)
- No guarantee if your script will work
I would also like to quote the PHP manual regarding the output of the check:
On success, the text No syntax errors detected in is written to standard output and the shell return code is 0. On failure, the text Errors parsing in addition to the internal parser error message is written to standard output and the shell return code is set to -1.
Example source file (lint.php)
<?php echo dqsf$qsdf
The actual example
$ php -l lint.php Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in lint.php on line 2 Errors parsing lint.php $
List modules (-m)
This option prints all the loaded PHP & Zend modules
Example
$ php -m [PHP Modules] xml tokenizer standard session posix pcre overload mysql mbstring ctype [Zend Modules]
Syntax highlighting (-s)
This option formats PHP code in highlighted HTML format. Needless to say this improves readability. The example below injects PHP code in the binary via an echo and prints the HTML version
Example
$ echo "<?php var_dump();" | php -s <code><span style="color: #000000"> <span style="color: #0000BB"><?php var_dump</span><span style="color: #007700">();<br /></span> </span> </code> $
Version info (-v)
Using the -v option you can easily display version information on your PHP compilation, the Zend Engine and additional patches. This information is quite straight forward and the example below is quite self-explanatory
Example
$ php -v
PHP 5.2.4-2ubuntu5.3 with Suhosin-Patch 0.9.6.2 (cli) (built: Jul 23 2008 06:44:49)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
with Xdebug v2.0.3, Copyright (c) 2002-2007, by Derick Rethans
$Function reflection (–rf)
The function reflection feature can be quite useful from time to time. It’s actually reflection of the function’s API. As the example below shows, the –rf reflects on all input and output parameters. You can’t really call it a manual, but it’s can serve as reference material anyway.
Notice: PHP should be compiled with reflection support to enable this feature
Example
$ php --rf var_dump
Function [ function var_dump ] {
- Parameters [2] {
Parameter #0 [ $var ]
Parameter #1 [ $... ]
}
}
$Class reflection (–rc)
The class reflection feature does exactly the same thing as the function reflection but in an object-oriented context. Again there’s reflection of the API, but this time the reference lists constants, properties & methods instead of functions. The example below illustrates the API reference of the Exception class
Notice: PHP should be compiled with reflection support to enable this feature
Example
$ php --rc Exception Class [ class Exception ] {
- Constants [0] { }
- Static properties [0] { }
- Static methods [0] { }
- Properties [6] {
Property [ protected $message ]
Property [ private $string ]
Property [ protected $code ]
Property [ protected $file ]
Property [ protected $line ]
Property [ private $trace ] }
- Methods [9] {
Method [ final private method __clone ] {
}
Method [ public method __construct ] {
- Parameters [2] {
Parameter #0 [ $message ]
Parameter #1 [ $code ]
}
}
Method [ final public method getMessage ] {
}
Method [ final public method getCode ] {
}
Method [ final public method getFile ] {
}
Method [ final public method getLine ] {
}
Method [ final public method getTrace ] {
}
Method [ final public method getTraceAsString ] {
}
Method [ public method __toString ] {
}
}
}
$Configuration information (-i)
Last but most definitely not least there’s rhe configuration information option which is the CLI version of the phpinfo() function: it prints information on the configuration of PHP and all of the compiled modules. Using a grep command, you can easily filter the data you’re looking for.
Example
This examples show you how to look for all configuration keys that contain the term max.
$ php -i | grep max log_errors_max_len => 1024 => 1024 max_execution_time => 0 => 0 max_input_nesting_level => 64 => 64 max_input_time => -1 => -1 post_max_size => 8M => 8M upload_max_filesize => 2M => 2M mysql.max_links => Unlimited => Unlimited mysql.max_persistent => Unlimited => Unlimited mysqli.max_links => Unlimited => Unlimited odbc.max_links => Unlimited => Unlimited odbc.max_persistent => Unlimited => Unlimited session.gc_maxlifetime => 1440 => 1440 $
No Comments