[thelist] Dynamic if statement in PHP

Chris Blessing webguy at mail.rit.edu
Tue Oct 22 12:37:01 CDT 2002


Gustavo-

You can use the eval() function
(http://www.php.net/manual/en/function.eval.php), as such (not tested!):

$testString = "$var1 = 'some string' && $var2 = 'some value'";

if(eval($testString)){
	...
}

The value you pass to eval is a string of PHP code, so build the conditional
statements dynamically ahead of time and pass em in there.

Give it a shot, I'm curious as to how it works out.

Chris Blessing
webguy at mail.rit.edu
http://www.330i.net

> Hi all!
>
> I'm struggling with the creatio of what I believe should be called
> "dynamic if statement" in php. I don't know if such thing exists.
>
> I have a query string that might have several variables, say from one to
> five, not always the same variables, and not always the same number of
> variables.
>
> I've managed to put the variable names and values in an array, and I'm
> able to generate the conditions depending on the variables I have.
>
> However I cannot modify the if statment depending on the variables I have.
>
> For example if my query string has two variables I want to perform an if
> statment similar to this:
>
> if ( condition1 AND condition2)
>
> but if the query string has three variables, then the if statement should
> look like this:
>
> if ( condition1 AND conditio2 AND condition3)
>
> I've tried using switch(), case, but it apparently doesn't like it.
>
> Any suggestions?
>
> Thanks a lot in advance.
>
>
> Gustavo Arizpe
> http://www.area.com.mx/estrategica
>




More information about the thelist mailing list