Skip to content

Instantly share code, notes, and snippets.

@turanct
Created October 7, 2014 06:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turanct/c725739b7aaa8f136d12 to your computer and use it in GitHub Desktop.
Save turanct/c725739b7aaa8f136d12 to your computer and use it in GitHub Desktop.
a PHP REPL in a tweet
<?php
while(1){echo"> ";$i=rtrim(stream_get_line(STDIN,512,PHP_EOL),';');try{@eval('print_r('.$i.');');}catch(Exception$e){print_r($e);}echo"\n";}
@turanct
Copy link
Author

turanct commented Oct 7, 2014

PHP REPL in a tweet

see https://twitter.com/tinydroptest2/status/519371916990484480

Usage

Stand-alone

Just run the script, and a repl will start:

$ php REPLinatweet.php

> $numbers = range(1, 10);
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)

> array_map(function($number) { return $number * $number; }, $numbers);
Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
    [5] => 36
    [6] => 49
    [7] => 64
    [8] => 81
    [9] => 100
)

> exit

With pre-defined context

If you want to run the repl inside some predefined context (to inspect some values, etc...), do it like this:

<?php
// Here's the context you want to inspect
$foo = 'bar';

// Require the REPL where you want to inspect some variables
require 'REPLinatweet.php';

// Here's some more code
  • if you run the repl, you can now just type $foo and hit enter to see 'bar'!
  • when you're done inspecting and changing variables and functions, type exit to stop the REPL and the script, or type break to stop the REPL but let the code after the REPL execute

isn't that awesome?

Other php awesomeness in a tweet:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment