vendredi 12 juin 2020

Is it possible to convert a string into the condition of an if statement in php

I am trying to create something where I have a function that is given the query and body of an if statement, but no matter what I make the body, it always returns true, how would I go about fixing this?

public function ifStatement($query, $body) {

    if ($query) {

       return $body;

    }

}
$string1 = '0 == 0';
$string2 = '0 == 1';
$string3 = '1 > 0';
$string4 = '1 < 0';

ifStatement($string1, 'Hello World!');        //Returns True
ifStatement($string2, 'Hello World!');        //Also Returns true
ifStatement($string3, 'Hello World!');        //Also Returns true
ifStatement($string4, 'Hello World!');        //Also Returns true

Is there a more efficient way of going about this, or is there a way I can convert the string into something the if statement could use? If somebody could help I would really appreciate it.

Cheers.

Aucun commentaire:

Enregistrer un commentaire