possible to forward if-expression in variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    #1

    possible to forward if-expression in variable?

    Hi Folks,
    I would like to forward an expression to an if-statement via a variable.
    Like:
    $a=10;
    $b=20;

    $expr="$a > $b";#this should be my if-expression

    if ($expr) do something;#test should be if($a > $b)

    Of course this does not work, as $expr is simply determined as being true.
    Is there any chance to get this working?
    Thanks in advance,
    Chris
  • Pedro Graca

    #2
    Re: possible to forward if-expression in variable?

    Chris wrote:[color=blue]
    > I would like to forward an expression to an if-statement via a variable.
    > Like:
    > $a=10;
    > $b=20;
    >
    > $expr="$a > $b";#this should be my if-expression[/color]

    Try this:

    <?php
    $a = 5;
    $b = 10;

    $expr1 = ($a > $b);
    $expr2 = ($a < $b);

    if ($expr1) echo 'expr1 is true';
    else echo 'expr1 is false';

    echo "<pre>\n\n\ n</pre>";

    if ($expr2) echo 'expr2 is true';
    else echo 'expr2 is false';
    ?>
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Erwin Moller

      #3
      Re: possible to forward if-expression in variable?

      Hi Chris,

      In addition to Pedro's posting:
      I find it usefull to use the following construct in such situations:

      $total = (($num<100) ? 12 : 50) * 1.5;

      The first expression is evaluated, if true it returns 12, if not 50.
      The result is multiplied by 1.5

      You can nest them as many times as needed.

      Regards,
      Erwin Moller

      Comment

      • Chris

        #4
        Re: possible to forward if-expression in variable?

        Hi Pedro, Hi Erwin,
        thanks for the proposals. But I might have not been clear enough.
        You hand over the expression as a TRUE/FALSE result. Of course this
        works (with one exception - if expression is FALSE you won't be able
        to have an else statement - at least in my hands it cancels the else
        fork).

        What I wanted is a little different (and more complicated). My
        if-expressions should be stored as a string, because I fetch them from
        a database. Now I want to use them in a PHP-context. [Reason for this
        is that I check plausabilities in forms, depending on the result of
        selected formfields - the according plausibilities are defined in the
        database]

        So, is there any chance to convert a string
        "$a > $b"
        into an expression/result
        TRUE/FALSE.
        ??

        Sorry for having been inprecise in my forst posting...
        Maybe there is an answer.
        Cheers,
        Chris
        [color=blue]
        > In addition to Pedro's posting:
        > I find it usefull to use the following construct in such situations:
        >
        > $total = (($num<100) ? 12 : 50) * 1.5;
        >
        > Try this:[/color]


        <Pedro snip>
        <?php
        $a = 5;
        $b = 10;

        $expr1 = ($a > $b);
        $expr2 = ($a < $b);

        if ($expr1) echo 'expr1 is true';
        else echo 'expr1 is false';

        echo "<pre>\n\n\ n</pre>";

        if ($expr2) echo 'expr2 is true';
        else echo 'expr2 is false';
        ?>
        </Pedro snip>

        Comment

        • michel

          #5
          Re: possible to forward if-expression in variable?

          lookup eval() from www.php.net

          greetz,

          Michel

          "Chris" <christian@1cli ck2art.de> wrote in message
          news:72402740.0 402270358.3c23e 41b@posting.goo gle.com...[color=blue]
          > Hi Pedro, Hi Erwin,
          > thanks for the proposals. But I might have not been clear enough.
          > You hand over the expression as a TRUE/FALSE result. Of course this
          > works (with one exception - if expression is FALSE you won't be able
          > to have an else statement - at least in my hands it cancels the else
          > fork).
          >
          > What I wanted is a little different (and more complicated). My
          > if-expressions should be stored as a string, because I fetch them from
          > a database. Now I want to use them in a PHP-context. [Reason for this
          > is that I check plausabilities in forms, depending on the result of
          > selected formfields - the according plausibilities are defined in the
          > database]
          >
          > So, is there any chance to convert a string
          > "$a > $b"
          > into an expression/result
          > TRUE/FALSE.
          > ??
          >
          > Sorry for having been inprecise in my forst posting...
          > Maybe there is an answer.
          > Cheers,
          > Chris
          >[color=green]
          > > In addition to Pedro's posting:
          > > I find it usefull to use the following construct in such situations:
          > >
          > > $total = (($num<100) ? 12 : 50) * 1.5;
          > >
          > > Try this:[/color]
          >
          >
          > <Pedro snip>
          > <?php
          > $a = 5;
          > $b = 10;
          >
          > $expr1 = ($a > $b);
          > $expr2 = ($a < $b);
          >
          > if ($expr1) echo 'expr1 is true';
          > else echo 'expr1 is false';
          >
          > echo "<pre>\n\n\ n</pre>";
          >
          > if ($expr2) echo 'expr2 is true';
          > else echo 'expr2 is false';
          > ?>
          > </Pedro snip>[/color]


          Comment

          • Henk Verhoeven

            #6
            Re: possible to forward if-expression in variable?

            Chris,

            ?Can't you use:

            if ( eval("return $expression;") )
            do something

            Greetings,

            Henk.

            Chris wrote:
            [color=blue]
            > Hi Pedro, Hi Erwin,
            > thanks for the proposals. But I might have not been clear enough.
            > You hand over the expression as a TRUE/FALSE result. Of course this
            > works (with one exception - if expression is FALSE you won't be able
            > to have an else statement - at least in my hands it cancels the else
            > fork).
            >
            > What I wanted is a little different (and more complicated). My
            > if-expressions should be stored as a string, because I fetch them from
            > a database. Now I want to use them in a PHP-context. [Reason for this
            > is that I check plausabilities in forms, depending on the result of
            > selected formfields - the according plausibilities are defined in the
            > database]
            >[/color]

            Comment

            • Chris

              #7
              Re: possible to forward if-expression in variable?

              Thanks Michel and Henk!

              I hadn't though about eval() - it's that easy.

              Cheers,
              Chris


              Henk Verhoeven <news@metaclass REMOVE-THIS.nl> wrote in message news:<c1olkg$mq $1@news4.tilbu1 .nb.home.nl>...[color=blue]
              > Chris,
              >
              > ?Can't you use:
              >
              > if ( eval("return $expression;") )
              > do something
              >[/color]

              Comment

              Working...