It's 2018, and you're a top-notch modern web developer, with a load of knowledge and tools right there at your disposal: Google and StackOverflow, debugger with a GUI, IDE with autocomplete, you name it. Occasionally, though, you still find yourself in a plain old text console on a remote server, or you have to do something without IDE, or there is no network connection... In such cases it might be helpful to feel comfortable in a simple terminal. In this post I'm going to list some switches for the PHP command that you can use to get information and some utilities.
Getting basic information about PHP
$ php -i
phpinfo()
PHP Version => 7.2.10-0ubuntu1
System => Linux awesome 4.18.0-10-generic #11-Ubuntu SMP Thu Oct 11 15:13:55 UTC 2018 x86_64
Build Date => Sep 13 2018 13:38:55
Server API => Command Line Interface
Virtual Directory Support => disabled
...
Full information about your PHP interpreter. Pipe the output to the Unix
less
command in order to get pagination and search:
php -i | less
. Type
Q
to exit the
less
shell. Some distros might lack
less
, in that case you may try
php -i | more
, which doesn't give you search but still has pagination.
Want a short summary of which PHP extensions you have? Just ask:
$ php -m
[PHP Modules]
calendar
Core
ctype
date
dom
ds
exif
...
More specific info about core and extensions' functions and classes
Full information about functions and classes provided by an extension:
$ php --re ds
Extension [ <persistent> extension #46 ds version 1.2.6 ] {
- Dependencies {
Dependency [ json (Required) ]
Dependency [ spl (Required) ]
}
- Classes [11] {
Interface [ <internal:ds> interface Ds\Hashable ] {
- Constants [0] {
}
- Static properties [0] {
}
...
Information on a specific class:
$ php --rc Ds\Vector
Class [ <internal:ds> <iterateable> final class Ds\Vector implements Ds\Sequence, Traversable, Countable, JsonSerializable, Ds\Collection ] {
- Constants [1] {
Constant [ public integer MIN_CAPACITY ] { 8 }
}
- Static properties [0] {
}
...
Same for a function:
$ php --rf fopen
Function [ <internal:standard> function fopen ] {
- Parameters [4] {
Parameter #0 [ <required> $filename ]
Parameter #1 [ <required> $mode ]
Parameter #2 [ <optional> $use_include_path ]
Parameter #3 [ <optional> $context ]
}
}
Utilities
Ever found yourself creating a dummy PHP file of just a few lines - only to be run once and then deleted? The
-a
switch might be what you're looking for:
$ php -a
Interactive mode enabled
php > var_dump(join(", ", [1, 2, 3]));
php shell code:1:
string(7) "1, 2, 3"
php >
It starts an interactive shell so you can type any PHP code and execute it straight away. Requires PHP to be compiled with
readline
support (most distros have that anyway).
Want a quick check for any parse/syntax errors in a PHP file? Use linter:
$ php -l test.php
PHP Parse error: syntax error, unexpected 'array_shift' (T_STRING) in test.php on line 4
Errors parsing test.php
It has a web-server!
Yes! Just in case you missed it, as of PHP 5.4.0, the CLI SAPI provides a built-in web server. Want a quick look at a web page generated by an app? Here you go:
$ cd /my_application/document_root
$ php -S localhost:8000
Then open http://localhost:8000/ in your browser of choice and enjoy!
Hope you also have enjoyed this reading. Your feedback and questions are always appreciated!