/    Sign up×
Community /Pin to ProfileBookmark

eval() and logical operators

I’m trying to evaluate an expression like this:

[code=php]
<?php
$logic = “&&”;
$expr = true;
$variable = eval(“$expr $logic true;”);
var_dump($variable);
// NULL

$logic = “==”;
$expr = true;
$variable = eval(“$expr $logic true;”);
var_dump($variable);
// NULL

?>
[/code]

But it’s completely eating it. I also tried “&&” instead of “==”.
I would like it to work like this:

[code=php]
$logic = “&&”;
$expr = true;
$variable = eval(“$expr $logic true;”);
var_dump($variable);
//bool(true)
[/code]

If it’s any help:

[code=php]
$logic = “&&”;
$expr = true; //pubic boolean
$variable =<<<EOS
$expr $logic true;
EOS;
$result = eval($variable);
var_dump($variable);
echo “n”;
var_dump($result);

/* $variable */
//string(10) “1 && true;”

/* $result */
//NULL
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 11.2010 — From the manual:
[B]eval()[/B] returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned.[/quote]
Also, a quote attributed to the creator of PHP:
If eval() is the answer, you're probably asking the wrong question.[/quote]
?
Copy linkTweet thisAlerts:
@eval_BadCode_authorDec 11.2010 — I may have made a mistake storing logical operators as strings in a database, but PHP already knows what ==, !=, >=, <= mean... There's just no way to cast a string as a logical operator other than to evaluate it.

What I had originally did (which works) is write out a big if/elseif/else pile of code, but it limits my options to !=. ==, >=, <=. It's also very very confusing. I get the impression that I'm recreating a worse version of eval().

It's like 120 lines of code, which could be 20.

I'll post my code once I see how I like the eval() solution. I'll let you compare them.
Copy linkTweet thisAlerts:
@NogDogDec 11.2010 — Could you do something like:
[code=php]
function compare($expr1, $operator, $expr2)
{
switch(strtolower($operator)) {
case '==':
return $expr1 == $expr2;
case '>=':
return $expr1 >= $expr2;
case '<=':
return $expr1 <= $expr2;
case '!=':
return $expr1 != $expr2;
case '&&':
case 'and':
return $expr1 && $expr2;
case '||':
case 'or':
return $expr1 || $expr2;
default:
throw new Exception("Invalid operator '$operator'");
}
}
[/code]
×

Success!

Help @eval_BadCode_ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...