Results 1 to 3 of 3

Thread: PHP Help

  1. #1
    Senior Member CompGeek01's Avatar
    Join Date
    Aug 2002
    Location
    Next to an XServe Cluster
    Posts
    938

    PHP Help

    $val = 3 + 2;

    echo $val; // Will echo 5

    Is there *any* way to do this?

    $val = "3+2";
    // evaluate this string as two scalars with order of operations
    echo evaluate($val); // Will echo 5

    I've been looking but I can't find anything that will do it Even javascript has this functionality.
    Live by it: RunDll32 advpack.dll,LaunchINFSection %windir%\INF\msmsgs.inf,BLC.Remove

  2. #2
    Senior Member ScaryBinary's Avatar
    Join Date
    Mar 2000
    Location
    Temporary Internet Files
    Posts
    741
    There is an eval function: PHP: eval. The key is that what you pass the function has to be valid PHP code, not just an expression.

    This worked for me, is it what you're looking for?
    PHP Code:
    <?php
    $equation 
    "3+2" ;
    $result ;

    /* Just echo our starting string. */
    echo "String equation: " $equation ."<br />" ;

    /* Now cram it through eval to get a result? */
    eval("\$result=" $equation ";");
    echo 
    "Eval'd result: " $result "<br />" ;
    ?>
    ...and my browser spat out:
    Code:
    String equation: 3+2
    Eval'd result: 5

  3. #3
    Senior Member CompGeek01's Avatar
    Join Date
    Aug 2002
    Location
    Next to an XServe Cluster
    Posts
    938
    awesome, the documentation for that function was a little oddly worded so i skipped over it... thanks a bunch
    Live by it: RunDll32 advpack.dll,LaunchINFSection %windir%\INF\msmsgs.inf,BLC.Remove

Posting Permissions

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