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