Evaluating a string as an expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eureka2050
    New Member
    • Jul 2008
    • 6

    Evaluating a string as an expression

    Hi,

    I am a PHP coder, recently ran into a bit of a problem trying to evaluate expressions in PHP.

    I have an expression which is stored in a string variable and when I try to evaluate it, it always returns a 'TRUE'

    Eg:
    [PHP]
    $str = '6.0 >= 8.5' ;

    if($str)
    {
    //do something
    }
    else
    {
    //do something else
    }[/PHP]

    The value(rather the expression) inside 'str' variable comes from the database and I am suppossed to check whether it returns a 'TRUE' or a 'FALSE', but it always evaluates to 'TRUE'..

    I also tried doing something like foll., still doesn't work, always returns 'TRUE'
    [PHP]$a = 6.0 ;
    $b = '>=' ;
    $c = 8.5 ;

    if( (boolean) $a .$b .$c)
    {
    //do something
    }
    else
    {
    //do something else
    }[/PHP]
    The code is always treating the expression as a string and evaluating to 'TRUE', can anyone help me with a way of making the string work as an expression..

    Many thanks,
    Sib
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Check out eval().

    There may be a more elegant way than this.

    Code:
    $exp = '1 > 6';
    
    $exp_eval = "if( $exp ) echo 'true'; else echo 'false';";
    
    eval($exp_eval);

    Comment

    • eureka2050
      New Member
      • Jul 2008
      • 6

      #3
      Hey Markus,

      Thanks a lot for the reply, this solution would have perfectly with my requirement, however I was in a hurry to submit the code hence as a workaround I simply took my exp-string and fired a mysql query to evaluate the result :-

      Code:
      $str = '1 > 6' ;
      $sql_exp = "SELECT $str as val" ;
      I knew this wasn't the best solution, but couldn't think of anything else at the time :)

      But, thanks a lot really, your help is much appreciated, will keep in mind for future reference.

      ---Sib.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Haha, okay.

        See you around.

        Mark.

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Eureka.

          Check out PHP's version_compare () function (http://php.net/version_compare).

          Comment

          • eureka2050
            New Member
            • Jul 2008
            • 6

            #6
            Originally posted by pbmods
            Heya, Eureka.

            Check out PHP's version_compare () function (http://php.net/version_compare).
            Hi,

            Thanks for responding.

            Yes, version_compare () too seems to be fine for evaluating simple expressions

            [PHP]if (version_compar e('6.0', '8.5', '>='))
            {
            echo 'yes' ;
            }
            else
            {
            echo 'no' ;
            } [/PHP]


            But fails for bigger expressions...

            [PHP]if (version_compar e("(6.0<=8.5) ", "(4<5)", "&&"))
            {
            echo 'yes' ;
            }
            else
            {
            echo 'no' ;
            } [/PHP]

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Looks like you need to nest your conditionals. Compare 6.0 and 8.5 first, then compare 4 and 5.

              Comment

              Working...