How to use the PHP interactive shell
Asked Answered
C

4

25

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?

Conservatoire answered 2/4, 2013 at 15:7 Comment(2)
Re "a lot of syntax is incorrectly interpreted": What syntax? Can you provide some examples?Motherless
OK, the OP is gone ("Last seen more than 8 years ago ").Motherless
N
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
Noisy answered 22/4, 2013 at 4:9 Comment(0)
N
10

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
Nicks answered 22/7, 2016 at 16:27 Comment(1)
You can install it without version specified. I.e. ... install php-readline. Currently if defaults to some 7.x version.Conurbation
N
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
Noisy answered 22/4, 2013 at 4:9 Comment(0)
F
7

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

Fiend answered 8/12, 2013 at 8:33 Comment(0)
A
2

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"
Af answered 18/5, 2016 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.