Wern Ancheta

Adventures in Web Development.

Quick Tip: Introduction to Phpsh

| Comments

In this quick-tip I’ll introduce you to a tool called phpsh. This project has been created for quite some time now. It was created at Facebook but is no longer actively being developed. But this shouldn’t stop you from using it. It’s a really important tool to have especially when you want to quickly test a PHP script. PHPsh is an interactive shell for PHP. It’s features include the following:

  • readline history
  • tab completion
  • quick access to documentation

You can download it directly from this link.

Once you’re done downloading phpsh, extract the zip file and navigate to the extracted folder. Then execute the following commands to install it in your system.

1
2
python setup.py build
sudo python setup.py install

Once that’s done, you can now open any terminal window and execute phpsh to start using it.

To get documentation regarding a specific function, use the d command followed by the name of the function you want to look up. Here are a few examples.

1
2
3
d echo
d array_push
d array_filter

It will then output the documentation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# echo

(PHP 4, PHP 5)

echo -- Output one or more strings

### Description

void echo ( string $arg1 [, string $... ] )

Outputs all parameters. 

echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo() (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses. 

echo() also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. This short syntax only works with the [short_open_tag][1] configuration setting enabled. 

` I have <?=$foo?> foo. `

### Parameters

arg1     

The parameter to output. 

...     

### Return Values

No value is returned. 

### 

   [1]: #ini.short-open-tag

Aside from directly accessing the docs, you can also write and execute any PHP code that you wish to test. Here’s a screenshot of me declaring an array and then looping through it.

phpsh

You can also connect to a database and perform operations on it.

db

That’s it for this quick-tip. I hope I’ve encouraged you to use phpsh on your projects.

Comments