Saturday, April 29, 2017

create_function

<?php
 $e = "n * 2";
 function incr($expression) {
  $expression = str_replace('n', '$n', $expression);  
  
  $func_body = "
   \$increment = (int)($expression + 1);
   return \$increment;";
   echo("Func body: " . $func_body);
  return create_function('$n', $func_body);
 }
 $increment_func = incr($e);
 echo $increment_func(2) . "<br/>";
?>
Output:
Func body: $increment = (int)($n * 2 + 1); return $increment;
5

No comments:

Post a Comment

here docs

$x = <<<THE_END PHP stands for "PHP: Hypertext Preprocessor". The acronym "PHP" is therefore, usually referre...