Results 1 to 6 of 6

Thread: Dynamic operators in if conditionals

  1. #1
    Join Date
    May 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Dynamic operators in if conditionals

    I am attempting to make an operator in an if conditional
    a variable like so:

    $x=1;
    $y=2;
    $operator='!=';

    if ($x $operator $y) {
    echo "Not equal!\n";
    }

    But obviously that doesnt work.
    Is it possible to convert a string to an operator?
    Any help would be appreciated.

    Thanks,

    Aphex_

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I don't mean to distract from your particular question, but this has bugged me for some time.

    In specific situations, it's nice to have variables actually change the php code itself.

    For example, it would be helpful if you could get a chunk of php from an outside source, like a database, depending on the situation.

    This operator question is a good example of it.

    At one point, I was playing around with a user-submitted math function (form with a text box they could enter "x+1" and expect a resulting set of numbers).
    But... didn't work, because I never was able to figure out how to set it up so that the input worked as math. I suppose with complex enough if statements and loops, I could use php to interpret the input, finding the various symbols and such, but that would be a lot of work.


    Unless someone knows something more, I think the best way to solve your problem if to just use a bunch of manually coded if statements.

    if ($operator == "!=" && $x != $y) {
    //do stuff
    }

    Then repeat for each possibility.




    So... anyone know if you CAN control the actual php script within itself?
    Last edited by djr33; 05-17-2006 at 06:54 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    May 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I got this working using the eval() function after someone suggested it to me.

    Thanks.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah. Good.

    I am still very interested in the concept, though.

    It's a worthwhile thing to look into for other uses.

    Glad its working
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes... the eval() function. The boon and curse of interpreted languages.

    It should almost never be necessary to use it.

    And you've made the newbie's mistake and confused = with ==.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah, thanks. Fixed the = thing... I'm tired.

    Will look into eval.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •