Re: Turning a string into a condition
| From: | Ashley Sheridan | Date: | Thu, 16 Feb 2012 20:48:02 +0000 |
| Subject: | Re: Turning a string into a condition | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-316636@lists.php.net to get a copy of this message | ||
On Thu, 2012-02-16 at 15:38 -0500, Marc Guay wrote:
> > It sounds like you have a string "$x < $y" in the database that you then
> > replace into a string "4 < 5" which you want to test a conditional on. If
> > this is the case, why are you storing conditionals in the database?
>
> The user will be able to construct their own query strings, it's
> complicated but necessary. The issue I'm dealing with, though, is
> this:
>
> $condition = "4 < 5";
> if ($condition){
> //do stuff
> }
>
You were on the right lines with the eval:
$condition = "return 4 < 5;";
if (eval($condition))
{
//do stuff
}
I just added the return statement and the semicolon, because the error
was complaining about it not being a proper PHP statement basically.
--
Thanks,
Ash
http://www.ashleysheridan.co.uk