How to use the PHP interactive shell

2019-02-06 09:19发布

问题:

I'm using Ubuntu 12.04 64 bit and I want to use the PHP interactive shell:

php -a

But it doesn't seem to work very well, 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?

回答1:

Try installing http://www.phpsh.org/ it is probably the easiest solution.

Steps: (assuming dependency's installed)

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


回答2:

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


回答3:

How to use the PHP interactive shell

phpsh was made by facebook. To install it see this: http://www.phpsh.org/

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:

el@apollo:~$ phpsh
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 yerself some info:

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

Explode parses the string on space into an array, print_r pretty prints it:

php> function little_bad_girl(){ print_r(explode(" ", "oxy contin")); }
php> little_bad_girl();
Array
(
    [0] => oxy
    [1] => contin
)

Foreach structure can be extended onto 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, 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
(
)

You want more??? There is enough to fill a lifetime, godspeed: http://php.about.com/od/advancedphp/



回答4:

How about PsySH ?

A litle example :

psysh 
Psy Shell v0.7.2 (PHP 5.5.12-2ubuntu4.6 — cli) by Justin Hileman
>>> $toto='ejgf5d78gfmkzl'
=> "ejgf5d78gfmkzl"
>>> substr($toto,0,2)
=> "ej"