Asked 6 years ago
1 Jun 2017
Views 4710
jessica

jessica posted

what is alternative of eval function in php ?

what is alternative of eval function in php ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

No alternative of eval function in php , means what eval function do in php no other function can do . it run php code
Rasi

Rasi
answered Nov 30 '-1 00:00

eval function run the given PHP code , you can do same by including that code like this by include file



function runEval($code){
 
file_put_contents("temp.php", $code);
ob_start();
include("temp.php");
$data=ob_get_contents();
ob_end_clean();
return $data;
}
runEval('echo "hello";');


it will print hello
because we copied code to a temp file and made it to include and buffered to output it.
so we made dummy eval function which is safe and sound we can put filter before code run.
so we made alternative of eval function .but it is not quite impressive as eval itself in PHP
Post Answer