PHP provides a handy support for System V IPC functions in the Semaphore module. Let’s have a look at the functions responsible for handling shared memory. They can be used to implement a simple way of exchanging information between the processes.
To see if your PHP installation supports IPC, run:

% php -i | grep sysv

If PHP was compiled with –enable-sysvmsg you should see something like this:

sysvmsg support => enabled

To actually user the shared memory we will:

  • Initialize shared memory with a size and a key. The key will be used as identifier of our shared memory block.
  • Store a variable in the shared memory. We will use another key to identify our variable within shared memory.
  • Read from shared memory and destroy it

I’m using CLI interface for all the examples below. The code:

$my_array = array(1,2,3);
$shm_id = shm_attach(SHMEM_KEY, 512);
if (!$shm_id) {
    echo "Failed to attach shared memory.\n";
if (!shm_put_var($shm_id, VARIABLE_KEY, $my_array)) {
    echo "Failed to put variable in shared memory $shm_id.\n";
$restored = shm_get_var($shm_id, VARIABLE_KEY);

Call to shm_attach above creates shared memory identified by number 0×12345 of the size of 512 bytes. It returns a handle that you then can use to operate on the shared memory. The handle is a resource of type sysvshm.
In the example above I did not destroy (remove) the shared memory. It means that it should stay on your system even after the script finishes. To “see” it on Linux you can use ipcs command:

% ipcs
------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x00000000 0          root       777        135168     1                       
0x00012345 3440670    zabuch     666        512        0             

Pay attention to the perms column. The default value (666) means that all other processes running on the same machine have read/write access the shared memory you just created.
Finally, use shm_remove($shm_id) to free the memory. All data will be destroyed.