tags:

views:

70

answers:

3
<?php
  $a = "a == a";
  eval($a);

This returns false. I thought it's supposed to return true. Any thoughts/ideas why this is so.

+5  A: 

Straight 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. If there is a parse error in the evaluated code, eval() returns FALSE and execution of the following code continues normally.

Looks like there is a syntax error somewhere in your string.

Etienne de Martel
`"a == a"` is not valid PHP code (unless `a` is a defined constant).
Andrew Moore
A: 

Still not sure about your intentions for the code, since there has been no response. If you're wanting to figure out if a variable is equal to a variable by the same name you can find out by doing this:

This returns true:

$a = $a == $a;
var_dump($a);

This returns false:

$b = 5;

$a = $a == $b;
var_dump($a);

Just a stab in the dark at why someone would write the block of code in the original post.

JMC
+1  A: 

I think this way of using eval() may work in other languages (JavaScript comes to mind), but it doesn't in PHP.

Issuing the command "Evaluate the following expression: a == a" makes sense and is right to expect true. But PHP's eval() doesn't work that way. It is a simple, primitive method to send code to the interpreter. If you eval()ed

eval("$b = 5; $a = $b == $b;");

$a would be true afterwards.

Pekka