SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: eval logic oly?

  1. #1
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,791
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)

    eval logic oly?

    This may sound naive, but maybe some expert knows a juicy PHP secret.

    I was wondering if there was a PHP function that can take a STRING and evaluate it as a logic statement, returning true/false?
    No, not EVAL as eval is evil. Essentially I wondered if there was a user accessible function that performs the evaluation in between () for conditional statements , but DOES NOT call functions or perform any PHP commands.

    maybe something that would look like this :

    PHPMagic("$a > $b || 2 === (1+1) "); // returns boolean true

    most likely what I am asking doesnt exist, but I wanted to make sure. Thanks all!

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $v = ( $a $b || $c === ( $d $e ) ); 
    Nothing fancy here.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,071
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    sure there is... just prepend $result = in front and run it through eval!

    Unfortunately, that is the only way I know of at the moment without getting "too clever" and using a system/exec call to run a command through php cli...
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,071
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    PHP Code:
    $v = ( $a $b || $c === ( $d $e ) ); 
    Nothing fancy here.
    Unless I missed something, I think you missed the STRING part, the equation is in a string form, so unless there is a cool way to get out of that, that I'm not thinking of, that wouldn't work.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Probably...Focused on going to work work at the moment. The only other solution I could think of is to use a parser in combination with the tokenizer: http://us3.php.net/manual/en/function.token-get-all.php
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,791
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    I'll have to figure about the tokenizer. am really not concerned about the variables as I already written a couple of nifty scripts that could hand those. I was just hoping to to have to parse through strings of unknown length for patterns of unknown complexity trying to extract operands and perform equations and/or concats if there was something that already did specifically that.

    The problem that am running into is that anything that could eval operands is also a system construct which means someone could inject actual PHP commands ... thus presenting tremendous security risk

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    The only other alternative, don't allow users or the like to submit such things.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

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