10 Examples To Inspire You To Use PHP Interactive Shell

Loading

Do you want to quickly test some PHP code?

Are you learning PHP?

Are you teaching PHP?

Don't miss out the PHP CLI Interactive mode. You can invoke PHP interactive shell from the terminal

php -a

Example 1: Do you want to see the output of xmlrpc_encode_request()

php > echo xmlrpc_encode_request('myIPAddress', array('username','password'));
<?xml version="1.0" encoding="iso-8859-1"?>
<methodCall>
<methodName>myIPAddress</methodName>
<params>
 <param>
  <value>
   <string>username</string>
  </value>
 </param>
 <param>
  <value>
   <string>password</string>
  </value>
 </param>
</params>
</methodCall>
php > 

Example 2: Do you want to see the list of registered socket transports?

php > print_r(stream_get_transports());
Array
(
    [0] => tcp
    [1] => udp
    [2] => unix
    [3] => udg
    [4] => ssl
    [5] => sslv3
    [6] => sslv2
    [7] => tls
)
php > 

Example 3: Do you want to see the default include_path?

php > echo get_include_path();
.:/user/lib/php:/usr/share/pear
php >

Example 4: Do you want to see the list of loaded extensions?

php > print_r(get_loaded_extensions()); 
Array
(
    [0] => libxml
    [1] => xml
    [2] => tokenizer
    [3] => session
    [4] => pcre
    [5] => SimpleXML
    [6] => sockets
    [7] => SPL
    [8] => shmop
    [9] => standard
    [10] => Reflection
    [11] => readline
    [12] => pcntl
    [13] => iconv
    [14] => hash
    [15] => gmp
    [16] => gettext
    [17] => ftp
    [18] => filter
    [19] => exif
    [20] => date
    [21] => ctype
    [22] => calendar
    [23] => bz2
    [24] => zlib
    [25] => openssl
    [26] => curl
    [27] => dbase
    [28] => dom
    [29] => gd
    [30] => imap
    [31] => json
    [32] => ldap
    [33] => magickwand
    [34] => mbstring
    [35] => mysql
    [36] => mysqli
    [37] => PDO
    [38] => pdo_mysql
    [39] => pdo_sqlite
    [40] => wddx
    [41] => xmlreader
    [42] => xmlrpc
    [43] => xmlwriter
    [44] => xsl
    [45] => zip
    [46] => xdebug
)
php > 

Example 5: Do you want to see if a given extension is loaded

php > echo extension_loaded('gd');
1
php > 

Example 6 Do you want to see the list of supported filters?

php > foreach (filter_list() as $value) { echo "\n$value"; }
int
boolean
float
validate_regexp
validate_url
validate_email
validate_ip
string
stripped
encoded
special_chars
unsafe_raw
email
url
number_int
number_float
magic_quotes
callback

Example 7: Do you want to know the maximum file upload size set in your php.ini?

php > echo ini_get('upload_max_filesize');
2M
php > 

Note: There might be a different php.ini for CLI in your system.

Example 8: Do you want to know the memory limit set in your php.ini?

php > echo ini_get('memory_limit');
128M
php > 

Example 9: Do you want to quickly test a function by copy/pasting an example code snippet straight from the PHP manual?

php > $input_array = array('a', 'b', 'c', 'd', 'e');
php > print_r(array_chunk($input_array, 2));
Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )
 
    [1] => Array
        (
            [0] => c
            [1] => d
        )
 
    [2] => Array
        (
            [0] => e
        )
 
)
php > 

Example 10: Do you want to see the PHP version?

php > echo PHP_VERSION;
5.2.9
php > 

Don’t you think using the PHP interactive shell just might make a difference? Yes or no, let me know.

About the author

Sudheer is an entrepreneur and software developer. Get more from Sudheer on Twitter.


How do I use Interactive Shell in Windows

Hi there,

Do you know how to use Interactive Shell in Windows? I typed "php -a", then I got a message saying that "Interactive mode enabled", then nothing else.
I tried to type something like "echo PHP_VERSION;", but nothing happened. Did I miss something? Thank you.

Rainman

Let's try to discover why it

Let's try to discover why it is happening on your computer. What version of Windows and PHP are you using? How did you install PHP?

I am working on Windows XP +

I am working on Windows XP + PHP 5.2.10

I download the zip file from

I download the zip file from php.net and extract it to c:\php folder. Then I changed something in php.ini based on some tutorial on the internet.

Okay. I give up. Sorry. The

Okay. I give up. Sorry.

The PHP manual says:
"If you compile PHP with the Readline extension (which is not available on Windows), you'll have a nice shell, including a completion feature (e.g. you can start typing a variable name, hit the TAB key and PHP completes its name) and a typing history that can be accessed using the arrow keys. "

This feature many not be available in Windows.

I came across few blog posts about alternate PHP shells on Windows. http://tr.im/EfGh is one among them.

Re: How do I use Interactive Shell in Windows

Maybe this wil help: After typing php -a and getting that "interactive mode enabled" message, enter a php open tag (<? or <?php), press enter and then enter php commands.

C:\Users\Karel>php -a
Interactive mode enabled
 
<?php
echo ini_get('include_path');
.;C:\server\libs_php;C:\server\php\pear;C:\server\php\pear\

This will work, however..if

This will work, however..if you have a parse error..it will kick you out of shell !! :(

How to do these commands in Mac Terminal

After entering the interactive shell you need to type commands starting with the opening php tag <?php followed by your commands and then you must also include the closing tag ?>. The way commands are presented in this post will not work.

For instance:
php > echo extension_loaded('gd');
will fail.

Instead, type:
<?php echo extension_loaded('gd');?>

The way the commands are

The way the commands are presented in the original post works, and your way does not work at all.

php -is not compiled with

php -is not compiled with readline on most prebuilded version of php
MacOSX Snow Leopard: no
Windows: no
Ubuntu 9.10: yes

nice post how can i open php

nice post

how can i open php files? im searching and testing some command syntax and i cant open some exampleFile.php for example

Run and open

If you want to edit a PHP script, just use any text editor to open the file, view and edit its contents. If you want to run the PHP script issue the command:

php path/to/the/php/script.php

PHP Interactive Shell is

PHP Interactive Shell is indeed very useful.
In most of the examples you have here, it is just one line of code, so for example you can just do:
php -r 'print_r(stream_get_transports());'
which I find to be easier if it is only one line. I generally use the shell for multi-line stuff where I have to define variables or use loops.

Setting up an interactive console for PHP in Notepad++

Maybe this is the best way to do it in Windows:

Reditt link

PHP CLI CRUD?

do you know of any basic web crud app for cli = access via web url/queries via cli?
wondering how much faster it may be
tia,

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>. The supported tag styles are: <foo>, [foo].

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
audiovisua_:
By submitting this form, you accept the Mollom privacy policy.