SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using if condition with eval??

    i have a function which gets the '>' or '<' operator sign as a parameter

    now i need to use this parameter in if-else condition

    i tried using eval but it always return true..

    how should i go about with this?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,184
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    flag it and use a control structure.

    PHP Code:
    function do_something($flag=0) {

        if(
    $flag === 0) {

            return 
    '< operation';

        } else {

            return 
    '> operation';

        }

    }

    echo 
    '<p>',do_something(),'</p>';
    echo 
    '<p>',do_something(1),'</p>'
    Last edited by oddz; May 27, 2009 at 02:28.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Is this something similar to what you're attempting?
    PHP Code:
    <?php
    function compare($iLeftValue$sOperator$iRightValue)
    {
        switch (
    true)
        {
            case (
    $sOperator === '>'):
                return (
    $iLeftValue $iRightValue);
            break;
            
            case (
    $sOperator === '<'):
                return (
    $iLeftValue $iRightValue);
            break;
        
            default:
                return 
    false;
            break;
        }
    }

    compare(5'>'10); #false
    compare(5'<'10); #true

    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •