eval() Function in PHP | Evaluate PHP String

Date Published: 27/08/2022 Published By: JaiSchool

This function expects PHP code as a string in its parameter and evaluates it.

It is helpful when you have stored some PHP code in the database, to run that code as PHP code you need the eval() function.

eval() function Syntax

eval($code_as_string);

Example

<?php


$php_code_string = 'echo "jaischool<br>";echo $_SERVER["REQUEST_URI"];';

eval($php_code_string);


?>

Make sure the code as a string does not have any opening and closing PHP tags.

Evaluate PHP code From mixed Html and Php string

<?php


$phphtml_code_string = '<!DOCTYPE html>
<html>
<head>
	<?php 
		$title = "I am title";
		$body = "This code will go in body";
	?>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title><?php echo $title; ?></title>
</head>
<body>

<?php echo $body; ?>

</body>
</html>';

eval(' ?>'.$phphtml_code_string.'<?php ');


?>

Publish A Comment

Leave a Reply

Your email address will not be published.