0

I was wondering if it would be possible to do this:

$var = '$something == $somethingelse';

if ($var) { // Say hello }

and have it mean the same thing as this:

if ($something == $somethingelse) { // Say hello }

I know this will not work at the moment, because if ($var) checks to see if $var is set or not. But what I'm asking is: is there a method to allow this? Would appreciate an answer.

EDIT: Basically I feel I should explain why I am doing this. I want users to be able to control when they get a notification, based off some data that one of their "channels" has saved.

For instance, a user only wants to get a notification when their collected data is bigger than 60. So I would get them to choose two fields:

First field input of their number (int or float)

Second field choises (equals, <, >, =<, =>)

The last thing in this if statement would be their data collected by their "channel".

I then want to be able to save their choice in a database, somehow, and then later act upon their choices.

9
  • eval function in PHP does this: php.net/manual/en/function.eval.php Commented Apr 4, 2012 at 16:04
  • @anubhava: be careful with eval. Commented Apr 4, 2012 at 16:05
  • 2
    This is often considered to be a very bad practice, and there's usually another way to do whatever it is you need from eval(). Commented Apr 4, 2012 at 16:06
  • Agreed eval is dangerous but that is the only built-in function to do this job. Commented Apr 4, 2012 at 16:07
  • NO, no, no....no! there is no need to do this....and if there is find a different way without having to resort to eval Commented Apr 4, 2012 at 16:29

4 Answers 4

4

The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for this - I'd generate these strings somewhere else in my web app, and therefore no user would ever be able to edit these themselves.
@Alfo: If you're generating this string yourself, I'd suggest changing that code. Instead of generating a single string, return say, an array. With the values and the operation, such as array('$something','==','$somethingelse'). Then just check what the operation is and run the correct if.
That's interesting. How would I then use that information to generate dynamic if statements?
@Alfo: I added an answer explaining my idea.
IMHO Rocket's idea would be a safer implementation, but you're design pattern and solution approach are still suspect.
0

Why not just use if ($something == $somethingelse) { // Say hello }? It's not that much more code.

3 Comments

@Alfo: Storing PHP code in a database is generally frowned upon.
@Rocket even if it's just $a=>$b?
@Alfo: Only because now you have to run it somehow.
0

If you come to this question, then there is something wrong with your design.

Mixing the code with data (what are you actually doing) is one of worst things a programmer can do. It will make your code unmaintainable and insecure.

You have to redesign your application to make your code solid and not variable.

Comments

-1

You say this string is being generated by you elsewhere. I suggest, instead of generating a string, generate an array.

Like this: array('something','==','somethingelse').

Now when you want to process this, you can replace the variables with their values, and then check the operator and run the correct operation.

Something like this:

$x = array('something','==','somethingelse');
list($valA, $operation, $valB) = $x;

// Replace values
$valA = $$valA;
$valB = $$valB;

// Run operation
$result = false;
switch($operation){
    case '==':
        $result = $valA == $valB;
        break;
    default:
        break;
}
if($result) echo ":-)";

DEMO: http://codepad.org/vaELABEm

You can even use this to do other things, by adding more cases to the switch.

1 Comment

Why a downvote? I think this is a much better idea than eval.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.