When you use a tool day in, day out, you take a lot of things for granted. Sometimes there are some really useful things available but because you don’t look, you miss them. That got me thinking about php.exe and what some alternative options are available which might be useful. This short article will highlight some of those and give some examples and outputs of their use, where appropriate. Let’s start with the most obvious.
-a Run as interactive shell -c <path>|<file> Look for php.ini file in this directory -n No configuration (ini) files will be used -d foo[=bar] Define INI entry foo with value 'bar' -e Generate extended information for debugger/profiler -f <file> Parse and execute <file>. -h This help -i PHP information -l Syntax check only (lint) -m Show compiled in modules -r <code> Run PHP <code> without using script tags <?..?> -B <begin_code> Run PHP <begin_code> before processing input lines -R <code> Run PHP <code> for every input line -F <file> Parse and execute <file> for every input line -E <end_code> Run PHP <end_code> after processing all input lines -H Hide any passed arguments from external tools. -S <addr>:<port> Run with built-in web server. -t <docroot> Specify document root <docroot> for built-in web server. -s Output HTML syntax highlighted source. -v Version number -w Output source with stripped comments and whitespace. -z <file> Load Zend extension <file>.
args... Arguments passed to script. Use -- args when first argument starts with - or script is read from stdin
--ini Show configuration file names
--rf <name> Show information about function <name>. --rc <name> Show information about class <name>. --re <name> Show information about extension <name>. --rz <name> Show information about Zend extension <name>. --ri <name> Show configuration for extension <name>.
Not sure why your PHP isn’t recognising some new v7 types of code? You need to check the version number.
> php -v PHP 7.2.5 (cli) (built: Apr 25 2018 02:39:21) ( ZTS MSVC15 (Visual C++ 2017) x86 ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans
Modules Loaded (-m)
This shows which modules are loaded with PHP. Very useful if you want to know if XDebug is installed, for example.
> php -m [PHP Modules] bcmath bz2 calendar Core ctype curl date dom exif fileinfo filter ftp gd gettext hash iconv intl json libxml mbstring mysqli mysqlnd openssl pcre PDO pdo_mysql pdo_sqlite Phar readline Reflection session SimpleXML SPL standard tokenizer wddx xdebug xml xmlreader xmlwriter zip zlib
[Zend Modules] Xdebug
We’ve all done something like this:
but did you know you can do it from the command line? Send the output to a file - it’s going to be big!
> php -i > info.txt
The Interactive Shell (-a)
With this, you can try out code snippets or even use it like a calculator!
> php -a Interactive shell
php > echo 1+2; 3 php >
Type “quit“ to exit.
Running Commands on the Command Line (-r)
Similar to the example above, you can run commands on the command line like so:
Hopefully that showed you something you haven’t seen before. Of course, check out the command line help or the manual link above for even more examples such as timing routines, checking syntax and starting in-built servers etc.
Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com