Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts

    eval and math functions

    There seems to be a rift between programmers whether eval on math strings works or it does not.

    PHP Code:
    $str="(6 - 4)";
    $num=eval($str); 

    My question is, should $num = 2, or is eval not the way to go?
    When I try the above, $num comes up empty.

    I have a function which splits off each number and sign, and then evaluates the math, but wanted to get a second opinion on the whole eval argument.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That would return null, but it has a parse error from the looks of it.
    Eval works (btw, eval is the ONLY function in PHP that I recall always fails for me the first time I write it) by parsing what its given as PHP code.
    The above will not work, since (6 - 4) is not evaluatable in PHP. What is evaluable is (6 - 4);, but still will result in null.
    In order to store the result, you must either return the data (oh jeez, this will probably fail the first time :P) with $str = "return (6 - 4);"; to return it to $num, or you can lump it as the result directly within the eval: eval("\$num = \"$str\";");.
    Crossing my fingers that either of those work on the first try hah! Also, try to limit you're usage of eval, since it is slower and open to exploitation.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    This should do it:
    PHP Code:
    $str '(6 - 4)';

    eval( 
    '$sum = ' $str ';' );

    var_dump$sum ); // int(2) 

  • #4
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    Found something similar and it works:

    PHP Code:
    $cur="(6 - 4)";
    eval(
    "\$value=".$cur.";");
    echo 
    $cur " = " $value "<br />"
    This prints (6 - 4) = 2.

    Thanks!

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by mathceleb View Post
    Found something similar and it works:

    PHP Code:
    $cur="(6 - 4)";
    eval(
    "\$value=".$cur.";");
    echo 
    $cur " = " $value "<br />"
    This prints (6 - 4) = 2.

    Thanks!
    How about mine, did either work? I'm just curious since I have yet to get eval to work the first time :$
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •