Re: variable if statement

From: Date: Thu, 23 Aug 2001 17:35:49 +0000
Subject: Re: variable if statement
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-11738@lists.php.net to get a copy of this message
<? $a=1; $flag = 0; $test = "(a==1) OR (b==1)"; $string = "if($test){ $flag =1;}"; var_dump($string); eval($string); if( $flag ==1 ) {var_dump($a);} ?> Produces: string(28) "if((a==1) OR (b==1)){ 0 =1;}" Parse error: parse error in c:/apache/htdocs/test2.php(7) : eval()'d code on line 1 Why? Because of using "" instead of '', or if use "" the escape the $ Also there is another problem: $test = "(a==1) OR (b==1)"; must be $test = "(\$a==1) OR (\$b==1)"; if it was $test = "($a==1) OR ($b==1)"; then there will be a substitution. As a side effect this will work because if $a=1 then $test="(1==1) or (what_is_b==1) which given to eval() will work. Andrey Hristov IcyGEN Corporation http://www.icygen.com 99% ----- Original Message ----- From: "Rick Emery" <remery@excel.com> To: <php-db@lists.php.net> Sent: Thursday, August 23, 2001 8:27 PM Subject: RE: [PHP-DB] variable if statement > eval will work, but the entire thing must be in the eval(). It would look > like: > > $flag = 0; > $test = "(a==1) OR (b==1)"; > $string = "if($test){ $flag =1;}"; > eval($string); > if( $flag ==1 ) {do_something;} > > I had to resort to this in a similar situation. I read the condition and > result action in a MySQL database. > > Richard Emery > Excel Communications, Inc. > IT Sr. Project Manager > (972) 478-3398 > (972) 944-0542 (pager) > > There is no "trying"... > There is only "Do" or "Not Do" > > > > -----Original Message----- > From: Brian Weisenthal [mailto:bsw10@cornell.edu] > Sent: Thursday, August 23, 2001 12:15 PM > To: php-db@lists.php.net > Subject: [PHP-DB] variable if statement > > > i want to do something like this: > > $string="($a==1) OR ($b==2)"; > > if ($string) { do something ; } > > any ideas? i tried using if (eval($string)) but that just gave me errors. > > thanks > > > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net > For additional commands, e-mail: php-db-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net > For additional commands, e-mail: php-db-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > >

« previous php.db (#11738) next »