How to use PHP from command Line?

As we all know that PHP is a server side scripting language and we usually run it behind the Web Servere like Apache . Since PHP is comparatively easy to learn, has less turn around time and have wide range of functions available for quicly building web pages, Its generally considered as a programming language for web and limited to it. But in reality, PHP has sereval type of Interfaces or SAPI (the Server API) like
apache, cli, cgi and so on.

In this post, we will focus on PHP using cli (Command Line Interface) interface and will learn some handful tricks and techniques.

Assumption: PHP is installed and path variables are set.

How to check which PHP version is installed?

To check PHP version from command line, simple type php --version

[code lang=”shell”]

[nerd@codezuzu:~] # php –version

PHP 5.3.10-1ubuntu3.9 with Suhosin-Patch (cli) (built: Dec 12 2013 04:27:25)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans

[/code]

How to check which modules are compiled with PHP?

[code lang=”shell”]

[nerd@codezuzu:~] # php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
ectoken
ereg
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
igbinary
json
libxml
mbstring
mcrypt
memcache
memcached
mhash
mysql
mysqli
OAuth
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_pgsql
pgsql
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
ssh2
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xdebug
xml
xmlreader
xmlwriter
xsl
zip
zlib

[Zend Modules]
Xdebug

[/code]

How to get information about a function in PHP?

To get the information on a function, do php --rf [function_name]

[code lang=”shell”]

[nerd@codezuzu:~] # php –rf in_array
Function[function in_array] {

– Parameters [3] {
Parameter#0[ $needle]
Parameter#1[ $haystack]
Parameter#2[ $strict]
}
}

[/code]

How to execute a PHP file from command line?

To execute a file from cli, do php -f [filename]


# Create a file called "codezuzuTest.php" and add some code to it.
[nerd@codezuzu:~] # touch codezuzuTest.php; echo '<?php echo "Hello codezuzu.com"; ?>' > codezuzuTest.php 

# Parse and execute the file
[nerd@codezuzu:~] # php -f codezuzuTest.php 
Hello codezuzu.com


How to lint (Syntax Check) a PHP file from command line?


# Create a php file and put some incorrect code in it. like in the example below `starting double quote` is missing
[nerd@codezuzu:~] # touch codezuzuTest.php; echo '<?php echo Hello codezuzu.com" ?>' > codezuzuTest.php

# Now try to lint that file, It should throw the syntax error
[nerd@codezuzu:~] # php -l codezuzuTest.php 
PHP Parse error:  syntax error, unexpected T_STRING, expecting ',' or ';' in codezuzuTest.php on line 1
Errors parsing codezuzuTest.php

How to run PHP code directly from command line?

To run PHP code directly from command line, do php -r [phpcode]


# Do some mathematics
[nerd@codezuzu:~] # php -r 'echo 25+25-1;'
49


# Print today's date
[nerd@codezuzu:~] # php -r "echo date('c');";
2015-03-06T17:44:48+05:30

We can also run php in interactive mode using php -a. For more information on command line options, do php --help.
There are lot that we can do using PHP from command line, that I will share in the further posts. So stay tuned and Happy coding.

Let me know your feedback and thoughts in comments.

One thought on “How to use PHP from command Line?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.