php-interact.php: evaluate single-line php expressions. Exit by calling `exit` or by typing the EOF character (^D on *nix, ^C on Windows). "; eval(fgets($fp)); } fclose($fp); It is fleshed out to treat I/O and error-messages a bit better. ***/ error_reporting(E_ALL|E_STRICT); $fileNamePattern = preg_quote(__FILE__,'/'); $errorSuffixPattern = preg_quote(": eval()'d code on line 1"); define("ERROR_MESG_PATTERN", "/" . " in $fileNamePattern\([[:digit:]]+\) $errorSuffixPattern" . "/" ); //echo "the error-message pattern is: $errorMesgPattern"; function myCleanUp($output) { $obviousNewline ="⏎\n"; // Unicode "Return Symbol" (U+23CE) $output = preg_replace("/\n/",$obviousNewline,$output); if (($output !== '') && substr($output,-1) !== "\n") { $output .= "\n"; } // Or, just always append \n? $outputShortenErrorMsg = preg_replace(ERROR_MESG_PATTERN,'',$output); return (($outputShortenErrorMsg === $output) ? $output : ('>>> '.ltrim($outputShortenErrorMsg))); // Approximations (e.g. bugs): // We assume that if __FILE__ and following error-like text appears anywhere in the output, // then it indicates an error actually occurred, and that // error-message has some extraneous whitespace at the very start of the output. // [We trim that and add ">>>"; if there were multiple errors or they weren't at the start, // we just muddled things.] // echo "hi",$undefdVar,"bye"; // // Future approach: add a try-catch around the code we eval? // (What about 'notice' which is already caught/handled?) } $fp = fopen("php://stdin", "r"); while(!feof($fp)) { echo "php> "; $oneLineIn = fgets($fp); ob_start(); eval(trim($oneLineIn)); echo myCleanUp(ob_get_clean()); } fclose($fp); /* Future work: Add namespace, and try-catch, around the code we eval? (I tried adding a separate namespace for the eval'd code, and this script itself, but the 'eval'd code could still grab variables from the script?! Sigh.) */ ?>