Welcome, guest | Sign In | My Account | Store | Cart

A small snippet that allows you to write/paste PHP code in your browser to have the server evaluate it and print the results. Useful for debugging and testing, it provides a PHP equivalent to Visual Basic's immediate window.

PHP, 40 lines
 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
<?php
function undo_magic_quotes ( $source )
{
  // in case magic_quotes_gpc is ON, we undo its effect ( "\'" => "'")
  $source = str_replace ( "\\'", "'",  $source );
  // "backslash double quote" becomes "simply double quote"
  $source = str_replace ( "\\\"", "\"",  $source );
  // "backslash backslash" becomes simply "backslash"
  $source = str_replace ( "\\\\", "\\", $source );

  return $source;
}

$actual_code = undo_magic_quotes ( $_POST["code"] );
?>
<html>
  <head>
    <title>
      iPHP
    </title>
  </head>
  <body>
    <form method="post">
      <textarea name="code" cols="80" rows="10" wrap="virtual"><?php
        echo htmlentities ( $actual_code, ENT_QUOTES );
      ?></textarea>
      <input type="submit" value="Execute" />
    </form>
    <hr />
    <pre><?php
      if ( strlen ( trim ( $actual_code ) ) > 0 )
      {
        echo "Results of execution:\n";
        flush ( );
        echo eval ( $actual_code );
      }
    ?>
    </pre>
  </body>
</html>

Writing some string parsing code or some tricky math and want to quickly test it to see if you're on the right track? (ie: to avoid off-by-one errors) Don't feel like creating a new file on the webserver, pointing your browser to it, seeing you got it wrong, going back to your text editor, changing something, re-saving, reloading your browser, etc., etc.?

Just paste this code in a new file (call it whatever you want), save it somewhere on your webserver, point your browser to it, paste your PHP code in the textarea and hit "Execute". The form will be submitted to itself and the results of your execution (including the return value) will be displayed under the textarea.

Code in that textbox must follow the rules set out by eval: http://www.php.net/manual/en/function.eval.php (ie: must end with a PHP statement and a semicolon...)

WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING

Do not leave a file that can blindly execute any code sent to it (like this program) accessible to the general public on a production web server! You have been warned! :)

1 comment

Dave Benjamin 19 years ago  # | flag

A similar script. Here's a script I used to use for similar purposes. The main difference is that mine evaluates expressions, not code blocks, and all results are formatted with print_r(). I used this to learn about PHP's many array-related functions.

&lt;?php $input = isset($_POST['input']) ? stripslashes($_POST['input']) : '' ?>

&lt;html>
&lt;head>
&lt;title>PHP Expression Evaluator&lt;/title>
&lt;/head>

&lt;body style="font-family: sans-serif">
&lt;h1>PHP Expression Evaluator&lt;/h1>

&lt;h2>Input&lt;/h2>
&lt;form action="&lt;?=$PHP_SELF?>" method="post">
&lt;textarea name="input" cols=80 rows=5>&lt;?=$input?>&lt;/textarea>
&lt;input type="submit" value="Go">
&lt;/form>

&lt;h2>Output&lt;/h2>
&lt;pre>&lt;?php if ($input) eval("print_r($input);") ?>&lt;/pre>
&lt;/body>
&lt;/html>
Created by Olivier Dagenais on Mon, 9 Sep 2002 (MIT)
PHP recipes (51)
Olivier Dagenais's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks