How to use the PHP interactive shell

后端 未结 4 1220
旧巷少年郎
旧巷少年郎 2020-12-31 06:05

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

相关标签:
4条回答
  • 2020-12-31 06:45

    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"
    
    0 讨论(0)
  • 2020-12-31 06:51

    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
    0 讨论(0)
  • 2020-12-31 06:59

    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
    
    0 讨论(0)
  • 2020-12-31 07:03

    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/

    0 讨论(0)
提交回复
热议问题