phpif-statementeval

Using a string as the condition of an if statement in PHP


I am trying to create a function that takes the condition of an if statement as a string and returns it as a boolean to use in an if statement. Once I have the boolean, I want to be able to return $body if is true.

The logic would look something similar to the below:

public function ifStatement($query, $body) {
    if (eval($query)) {
       return $body;
    }
}

So when using the function, the output would look something like this where if the condition is evaluated and if the condition is true, it would return the $body, but if it's false it would return nothing.

ifStatement('true', 'Hello World!');         // Returns 'Hello World!'
ifStatement('false', 'Hello World!');        // Returns nothing
ifStatement('0 == 0', 'Hello World!');       // Returns 'Hello World!'
ifStatement('0 == 1', 'Hello World!');       // Returns nothing

The primary reason I am looking for something like this is so because I am creating a template engine where I want to be able to pass if statements and other logic statements into php as a string and have it return the appropriate values.


Solution

  • Using eval to evaluate the string like that will not work because it is not valid php syntax. Instead of using eval($query), try using eval("return $query;") that way you are actually returning the value as a boolean instead of using the string itself in the condition of the if statement.

    So the revised code would look something like this:

    function ifStatement($query, $body) {
    
        $condition = eval("return $query;");
        
        if ($condition) return $body;
        return;
    }
    

    In that new function you are able to take in whatever condition you want and it will be evaluated as a boolean. So then the resulting outputs would like like this:

    print ifStatement("true", "This is true");
        # Returns "This is true"
    
    print ifStatement("false", "This is false");
        # Returns nothing
    
    print ifStatement("1 == 1", "1 is 1");
        # Returns "1 is 1"
    
    print ifStatement("1 == 2", "1 is not 1");
        # Returns nothing
    

    Adding a false condition

    If you wanted to go a step further, you could modify the function a little bit more to include what is returned in the event that the condition is false. All you would need to do is add a new optional param which would be what is returned if the condition is false. It would look like this:

    function ifStatement($query, $trueBody, $falseBody = "") {
    
        $condition = eval("return $query;");
        
        if ($condition) return $trueBody;
        else return $falseBody;
    }
    

    With the outputs looking like this:

    print ifStatement("true", "This is true", "This is false");
        # Returns "This is true"
    
    print ifStatement("false", "This is true", "This is false");
        # Returns "This is false"
    
    print ifStatement("1 == 1", "1 is 1", "1 is not 1");
        # Returns "1 is 1"
    
    print ifStatement("1 == 2", "1 is 1", "1 is not 1");
        # Returns "1 is not 1"