Eval () PHP Function, Example of Eval () PHP Function


 

Eval () PHP Function, Example of Eval () PHP Function

Learn how to use Eval () PHP Function to execute PHP code.

Learn how to use Eval () PHP Function to execute PHP code.

PHP eval() Function

In this tutorial we will learn about the PHP eval() function. The php eval() function can be used if you want to store the php command in MySQL table or in text file and then run the same in a PHP script.

The PHP eval() function:

The PHP eval() function evaluates the passed string parameter as PHP code. The string parameter must be a valid PHP code and end with a semicolon, just like regular PHP code.

After the evaluation of the string it returns NULL value if there is no return statement in the string. If there is some parse error in the string the eval() function returns FALSE.

Syntax of eval() function:

eval(codeasstring)

Parameter Description
codeasstring The parameter is required and it must be valid PHP code.

Example code of eval() function:

The following example how to use eval() function.

<?php
$string = "Hello";
$today = date("F j, Y, g:i a");

$str = 'Say $string to PHP. Today is $today';
echo $str. "<br />";

eval("\$str = \"$str\";");
echo $str;
?>

The out put of the program is:

Say $string to PHP. Today is $today
Say Hello to PHP. Today is August 31, 2010, 4:40 am

Ads