Running php eval on code with tags

php eval on code wrapped in php tags

Here is the code snippet which calls php eval on a code which is wrapped in <?php and ?> tags.

<?php
$code = '<?php echo "hello world\n";?>';
echo "Doing eval...\n";
eval($code);
echo "Done...\n";
?>

The outcome from above code is:

Doing eval...
Done...
Env: PHP 5.5.9 (Linux)

php eval on code wrapped in php tags after prepending “?>”

Here is the code snippet which calls eval on a code which is wrapped in <?php and ?> tags after prepending it with “?>”

<?php
$code = '<?php echo "hello world\n";?>';
echo "Doing eval...\n";
eval('?>' . $code);
echo "Done...\n";
?>

The outcome from above code is:

Doing eval...
hello world
Done...
Env: PHP 5.5.9 (Linux)

Note that by default eval expect code without tags. But there are situation we need to execute a code which may have php tags (e.g. code read from a php file). In that case this approach can be used.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments