Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword eval

Execute PHP source text in the variable scope of the call.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

eval('return $n + 1;')

Execute PHP source text in the variable scope of the call. Use a trusted fixed expression without opening PHP tags. A return within the text supplies the result of eval, otherwise the result is null. Invalid syntax raises ParseError in PHP 8.4.

$n=4; $result=eval('$n += 3; return $n * 2;');
echo json_encode([$n,$result]); try {eval('return (;');} catch (ParseError $e) {echo " ",get_class($e);}
[7,14] ParseError

Examples

<?php
$n=4; $result=eval('$n += 3; return $n * 2;');
echo json_encode([$n,$result]); try {eval('return (;');} catch (ParseError $e) {echo " ",get_class($e);}

Result

[7,14] ParseError

Sources