IPHP: A PHP repl based upon the IPython notebook.

As written in the post IPython the IPython architecture allows swappable kernels based upon any programming language you want. This is a blog post about IPython-PHP/IPHP, a kernel for the PHP programming language.

Try out IPHP

First I will describe how to checkout IPHP, a kernel for PHP. The second part will describe the architecture of kernels and how they communicate with the notebook.

Please install ipython: IPython The version 2.1 works fine, which is available through pip/conda. If you want to make your life easy install python and all its libraries via anaconda.

Next you need to get IPHP (ipython-php):

git clone git@github.com:dawehner/ipython-php.git
cd ipython-php
composer install

Now start an ipython instance with a preconfigured kernel:

ipython notebook --KernelManager.kernel_cmd="['php', 'path-to/ipython-php/ipython_php.php', '{connection_file}']"

There you press the "new notebook" button. Click on the first cell and enter some php code:

$a = 123;
$b = 124;

print $a * $b;

It should look a bit like the following: ipython PHP

If you play around with it you will find a lot of problems:

  • You can't reuse variables in the next cell block. This should be solveable.
  • There is no proper syntax highlighting yet.
  • You can't execute a cell twice if it contains a function definition. This could be rather hard to solve.
  • std_err is supressed at the moment. This could be done.
  • We could support all the crazyness of IPython like shared variables, binary responses: images, raw HTML etc.
  • many more

It would be interesting to use boris internally: boris

This could solve a couple of these problems, at least the first one. Feel free to pay with it, report bugs and contribute!

In the following some short overview of the architecture will be given so you could somehow understand why it is possible to integrate any programming language into IPython.

IPython architecture.

IPython comes with a couple of frontends. On of which is the HTML based notebook, another one is the ipython console, more like a usual REPL and the QTConsole, an actual GUI application.

These frontend talks with a so called (language) kernel via ZeroMQ, a nice library with abstractions for sockets. There are a couple of sockets you need to open:

  • Heartbeat: Just used to ensure that the kernel still exists
  • Shell: The central socket on which messages with the actual executed code is passed to the kernel.
  • Stdin: used for example for things like a readline() or interactive widgets.
  • IOPubc : Used to send back stdout and stderr to the frontend.

Take a look at the ipython_php.php file for a hopefully understandable implementation of it.

Have fun!

Tags: ipython, php

Non blocking IO in php

Non-Blocking IO

There used to be a time where everyone freaked out about node.js [0]. It used a non-blocking and event driven programming module which potentially allowed a lot of concurrent connections. In contrast to node usual PHP based projects uses a blocking programming model. This also has advantages in terms of understandability of the code because execution is kinda linear.


In case you want to experiment with non-blocking code or just have an easier time with listening to multiple different sockets at the same time, there is a nice php library which provides the needed abstractions, so you don't have to fiddle with low-level details like libevent, libev or just select system calls, see [1]

React php logo

Example HTTP server.

You can really easy start with a non-blocking HTTP hello world application:

$ composer init --require=react/http:0.3.* -n
$ composer install

Add a new file looking like the following:

require 'vendor/autoload.php';

$app = function($request, $response) {
  $response->writeHead(200, array('Content-Type' => 'text/plain'));
  $response->end("Hello World\n");

// Abstraction for event loops (by default you don't need any external library).
$loop = React\EventLoop\Factory::create();
// Connects an socket to the event loop.
$socket = new React\Socket\Server($loop);

// Creates a HTTP server with an event subscriber listening
// to incoming requests.
$http = new React\Http\Server($socket, $loop);
$http->on('request', $app);

echo "Server running at\n";


Now just run this script with php and your are done.


With react it is quite simple to start with non-blocking IO/events though the question is how useful it is. There always existed problems with long-running PHP processes so don't blindly use this tool.

[0] http://nodejs.org/ [1] http://reactphp.org/

Tags: php, nonblocking

Interactive computing in the browser: IPython

explorational computing

Most programmers write a bunch of code in their editor/IDE of choice (compile) run it and look at the result. This works quite fine for tasks where you already know what you want to do, but it doesn't fit into the model of exploration.

What is exploration: Under exploration I do understand the process of playing around with things: data, a new library or just a programming language. For many languages you have the concept of a REPL (read eval print loop) which allows you to just type statements and look into the result without the compile/run step. Here is an example in PHP:

$ php -a
Interactive mode enabled

php > print "123";
php > $a = "hello";
php > $b = "world";
php > print $a . " " . $b;
hello world

While this is really handy for a quick start it becomes a little bit annoying if you have to re-execute previous lines or simply store the result to share it with other people.


Galileo Galilei, often refered as "father of modern science" used his papers to collect his observations, invented theories and tried proove them with this collected data. In a programming world you could think of writing documentation for a library with some text, adding some code and showing the results in one place galileo Galilei notebook

The python took that idea and came up with a wonderful tool: the IPython notebook.

Ipython logo

It provides a browser based tool which allows you to create notebooks. Each notebook consists of a bunch of cells, some for documentation with markdown, some for code and some for showing output of the code. Here is a really basic example of writing a blog post in a notebook:


A more complex example can be found on exoplanets. It contains markdown, shell commands, python and plots.

As the name says it is using python, though in reality it can talk with any programming language through a specified protocol via a so called kernel. The next blog post will be about adding a new kernel for PHP, so you basically get a new browser based PHP repl.

Tags: ipython, php, jupyter