Dynamic IF statements using php
Posted Date:26-08-2017
In this post we will explain eval and Dynamic IF statements using php
Eval:
eval — Evaluate a string as PHP code.
- Php code must be properly terminated using a semicolon.
- A return statement will immediately terminate the evaluation of the code.
Syntax:
eval(phpcode)
Example:
Simple example of eval php using simple variables
$string = "beautiful";
$time = "world";
$str = 'This is a $string $time morning!';
echo $str. "<br>";
eval("\$str = \"$str\";");
echo $str;
http://demo.pheonixsolutions.com/php/dynamic-if-condition/simple-eval.php
Second example, We are creating dynamic if condition using array with implode functions.
$var1 = 11;
for ($i = 0; $i <10 ; $i++)
{
$condi[]=('($var1<$i)');
}
$check=implode('||', $condi);
if(eval("return $check;"))
{
echo 'i is small value';
}
else
{
echo 'i is big value ';
}
