22

I'm using Ubuntu 12.04 (Precise Pangolin) 64 bit, and I want to use the PHP interactive shell:

php -a

But it doesn't seem to work very well, and a lot of syntax is incorrectly interpreted.

When I run php -a, it displays:

interactive mode enabled

And just a cursor blinking.

I'm using: PHP 5.4.13-2~precise+1 (cli) (built: Mar 21 2013 12:17:18)

How do I use the PHP interactive shell?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mohur
  • 1,785
  • 4
  • 16
  • 25

4 Answers4

9

This is what you'll get when the php5-readline package is not installed. Assuming that's your problem you can fix it by running this command:

sudo apt-get install php5-readline
SQB
  • 3,926
  • 2
  • 28
  • 49
mmalone
  • 1,122
  • 9
  • 10
8

Try installing phpsh. It is probably the easiest solution.

Steps (assuming dependencies are installed):

  1. git clone https://github.com/facebook/phpsh
  2. cd phpsh
  3. sudo python setup.py install
  4. phpsh
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
P4ul
  • 770
  • 5
  • 15
6

How to use the PHP interactive shell

phpsh was made by Facebook. To install it, see phpsh -- an interactive shell for PHP.

Installation directions:

sudo apt-get install git
cd /home/youruser;

Pull the repository, cd into it and install:

git clone https://github.com/facebook/phpsh
cd phpsh
sudo python setup.py install

Run it:

phpsh

Session:

Starting php
type 'h' or 'help' to see instructions & features
php>

Walkthrough:

Printing strings:

php> echo 'hi';
hi

Do some math:

php> echo 1+2;

3

Print some builtin variables:

php> echo $_SERVER;

Array

Print contents of that array:

php> print_r($_SERVER);

Array
(
    [LANG] => en_US.UTF-8
    [TERM] => xterm
    [SHELL] => /bin/bash
)

Get a key of that array:

php> echo $_SERVER['TERM'];

xterm

Addition of a different kind:

php> =2+2

4

Print the previous:

php> = $_

4

Store a variable:

php> $msg = "don't just sit there fancy pants, take the wheel";

php> echo $msg;

don't just sit there fancy pants take the wheel

An equation can be held open through newlines until it completes:

php> =2+
 ... 3+
 ... 4+5
14

Define our own arrays:

php> $derp = array(1,2,3);

php> echo $derp

Array

Get the type of a variable:

php> echo gettype(PHP_VERSION);

string

For great justice, loops:

php> $i = 0; while ($i < 3){$i++; echo "pinkie pie is best pony ";}
pinkie pie is best pony pinkie pie is best pony pinkie pie is best pony

Get yourself some information:

php> phpinfo();

phpinfo();
PHP Version => 5.3.10-1ubuntu3.8

Function explode() parses the string on space into an array, and print_r() pretty prints it:

php> function little_bad_girl(){ print_r(explode(" ", "oxy contin")); }
php> little_bad_girl();

Array
(
    [0] => oxy
    [1] => contin
)

A foreach structure can be extended onto the following lines.

php> foreach (array(1,2,3) as $item) {
 ... echo $item;
 ... }

123

Block comments are ignored:

php> /* echo "hidden"; */
php>

Read from a file:

php> $section = file_get_contents('/home/el/myfile.txt');
php> echo $section;

we will become a spacefaring civilization.

No, no time:

php> echo time();

1386492405

Pure sweet truth:

php> echo isset($_SERVER);

1

Make an array, and search for an item in it.

php> $data = array(0, 1, 2);

php> echo preg_grep("/1/", $data);

Array

php> print_r( preg_grep("/1/", $data));

Array
(
    [1] => 1
)

php> print_r( preg_grep("/4/", $data));

Array
(
)

Do you want more??? There is enough to fill a lifetime, godspeed: PHP and MySQL Tutorials

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
1

Use PsySH:

A little example:

psysh

Psy Shell v0.7.2 (PHP 5.5.12-2ubuntu4.6 — cli) by Justin Hileman

Session:

>>> $toto='ejgf5d78gfmkzl'
=> "ejgf5d78gfmkzl"

>>> substr($toto, 0, 2)
=> "ej"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SebMa
  • 4,037
  • 29
  • 39