Kinz
Kinz

Reputation: 310

Evaluate a String in PHP

Well. I'm having a problem with the eval(); function in PHP. I don't quite understand how to store the returned data into a variable to print.. my code is as follows:

<?php 
    $a = 4; 
    $write = eval("$a+$a;"); 
    echo $write; 
?>

I'm not sure what I'm doing wrong. When I run the PHP script, all it does is output nothing.. Any help is appreciated

Upvotes: 0

Views: 331

Answers (4)

sulabh
sulabh

Reputation: 1117

<?php 
    $a = 4; 
    $write = eval("return $a+$a;"); 
    echo $write; 
?>

Upvotes: 0

Milap
Milap

Reputation: 7211

<?php 
    $a = 4;
    eval("\$write = \$a+\$a;");
    var_dump($write); 
?>

It will return int(8)

Upvotes: 0

Joseph
Joseph

Reputation: 119827

try eval("\$write = \$a+\$a;")

http://php.net/manual/en/function.eval.php

Upvotes: 0

animuson
animuson

Reputation: 54719

From the PHP documentation:

eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned.

Upvotes: 4

Related Questions