Boris Guéry

Hacking the web since 1997.

Phpsh - Console Interactive PHP

Quand j’utilise Python, c’est simple :

1
bguery@joyless:bin $ python

Et j’obtiens :

1
2
3
4
5
6
7
8
9
10
11
12
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = "Hello World"
>>> prin foo
  File "<stdin>", line 1
    prin foo
           ^
SyntaxError: invalid syntax
>>> print foo
Hello World
>>>

Avec php, je dois faire ça :

1
bguery@joyless:bin $ php -a

Et j’obtiens ça :

1
2
3
4
5
6
7
Interactive shell

php > $foo = "Hello World";
php > var-dump($foo);
PHP Parse error:  syntax error, unexpected T_VAR in php shell code on line 1
php > vardump($foo);
PHP Fatal error:  Call to undefined function vardump() in php shell code on line 1

Certes, je suis vraiment idiot, mais il faut le reconnaitre, ce n’est pas évident de tester de petit bout de code en PHP, il existe bien codepad, mais c’est pas le plus pratique.

La console interactive de PHP, n’est pas, réellement, interactive.

Et récemment j’ai découvert phpsh, un petit utilitaire qui remplace le shell interactive de PHP écrit en partie en Python par Facebook, en apportant quelques options bien pratiques.

Exemple:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
php> $foo = "Hello World"
php> vardump($foo)
Not executing input: Possible call to undefined function vardump()
php> d array_merge

# array_merge

(PHP 4, PHP 5)

array_merge -- Merge one or more arrays

### Description

array array_merge ( array $array1 [, array $array2 [, array $... ]] )

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way.

### Parameters

array1

Initial array to merge.

array

Variable list of arrays to recursively merge.

### Return Values

Returns the resulting array.

###

php> = 2 +
 ... 2
4
php> = array(array(1,2,3), array("a" => "b", "c" => "d"), "e", "g")
array(
  0 => array(
    0 => 1,
    1 => 2,
    2 => 3,
  ),
  1 => array(
    "a" => "b",
    "c" => "d",
  ),
  2 => "e",
  3 => "g",
)
php> q

Un regret tout de même, pas d’auto-complétion.

Les sources sur github.