Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the interactive PHP shell work?

Tags:

php

console

I am in the interactive shell for PHP using the command php -a in a terminal, but no commands are working. I even try a simple 2 * 2, and I get no results.

What am I doing wrong and how does it work?

like image 947
Cool Guy Yo Avatar asked Feb 09 '13 23:02

Cool Guy Yo


2 Answers

On the documentation for the interactive shell, the first note by Ryan P. has some notable information:

Interactive Shell and Interactive Mode are not the same thing, despite the similar names and functionality.

If you type php -a and get a response of 'Interactive Shell' followed by a php> prompt, you have interactive shell available (PHP was compiled with readline support). If instead you get a response of 'Interactive mode enabled', you DO NOT have interactive shell available and this article does not apply to you.

So if you get only "Interactive mode enabled", then you'll only be able to type in PHP code and then when you're done, send PHP an EOF to execute it.

This is probably not what you want. You may want to look into phpsh instead.

like image 183
icktoofay Avatar answered Nov 15 '22 23:11

icktoofay


How to use the PHP interactive shell

The php -a you speak of is a piece of crap.

phpsh was made by facebook.

Install git, go to where you want to build phpsh:

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> 

Assign Hour into a variable and print:

php> $t = date("H");
php> echo $t;
04

everyone loves if statements:

php> if (false){echo "derp"; } else if(true){ echo "foobar"; } else{echo "moo"; }
foobar

Get the length of a string:

php> echo strlen("012345678");
9

Sorting:

php> $numbers=array(4,6,2,22,11);
php> sort($numbers);
php> echo $numbers;
Array
php> print_r($numbers);
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 11
    [4] => 22
)

Sort by keys:

php> $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
php> ksort($age);
php> print_r($age);
Array
(
    [Ben] => 37
    [Joe] => 43
    [Peter] => 35
)

Push values onto a stack and pop them off.

php> $a[]="one"
php> $a[]="two"
php> print_r($a);
Array
(
    [0] => one
    [1] => two
)
php> echo array_pop($a);
two
php> echo array_pop($a);
one
php> echo array_pop($a);
php>

Basename gets the word on the right:

php> echo basename("http://foobar/helicase");
helicase
php> echo basename("/home/el/polymerase");
polymerase

Touch a file and get the last time it was modified:

php> touch("/home/el/myfile.txt");
php> echo filemtime("/home/el/myfile.txt");
1386494608

Love me some json:

php> $myjson = '{"a":1}';
php> var_dump(json_decode($myjson));
object(stdClass)#2 (1) {
  ["a"]=>
  int(1)
}

Several ways to grab something from inside json:

php> $json = '{"foobar": 123}';
php> $obj = json_decode($json);
php> print $obj->foobar;
123
php> print $obj->{'foobar'};
123

Function, JSON validator

php> function isJson($string) {
 ...  json_decode($string);
 ...  return (json_last_error() == JSON_ERROR_NONE);
 ... }

php> echo isJson("");
1
php> echo isJson("{}");
1
php> echo isJson("abc");

php> echo isJson("{'a': 1}");

php> echo isJson('{"a": 1}');
1
like image 24
Eric Leschinski Avatar answered Nov 15 '22 21:11

Eric Leschinski