assigning variables in e.g. an if ....

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

    assigning variables in e.g. an if ....

    Hi

    I have been looking into php.net, but could not find any proper
    description.
    There is a way of assigning variables from functions, while at the
    same time using them in e.g. an if.
    I have tried to use this, but failed.... and I have not found any
    proper information about this?

    E.g., if I am right, then

    if(strpos($line , '='))

    could be

    if( ($i=strpos($lin e, '=')) !== false )
    echo $i;

    but just how does this work? the parantheses do it?
    I asume I have to understand that as a statement in a statement?

    WBR
    Sonnich

  • Geoff Berrow

    #2
    Re: assigning variables in e.g. an if ....

    Message-ID:
    <5f0aa7b8-0fe4-4b34-8f81-f288cb0aebec@59 g2000hsb.google groups.comfrom
    jodleren contained the following:
    >I have tried to use this, but failed.... and I have not found any
    >proper information about this?
    >
    >E.g., if I am right, then
    >
    if(strpos($line , '='))
    >
    >could be
    >
    if( ($i=strpos($lin e, '=')) !== false )
    echo $i;
    >
    >but just how does this work? the parantheses do it?
    >I asume I have to understand that as a statement in a statement?
    Not sure I understand your question. AIUI parentheses work the same way
    they do in mathematics, ie the code inside is evaluated first. The
    manual tells you what a function returns (note: some functions don't
    return anything more meaningful than true or false). That value can be
    assigned to a variable.

    Now the manual entry for strpos is this
    "Returns the position as an integer. If needle is not found, strpos()
    will return boolean FALSE." Note the warning, if the character being
    searched for is at the beginning of the string it could return 0, or in
    other words a non-boolean false.

    So in the first example the 'if' will be executed if the character is
    found,as long as the character is not at position 0. To check for that
    you'd need

    if(strpos($line , '=')!==false)

    In the second example the bit in parentheses is evaluated first
    $i=strpos($line , '=')

    $i now contains the output of strpos, an integer if the character is
    found, false if not.

    So now the parentheses have been evaluated we effectively have:

    if( $i !== false )
    echo $i;

    in other words $i will be echoed as long as it is not false.








    --
    Geoff Berrow 011000100110110 0010000000110
    001101101011011 001000110111101 100111001011
    100110001101101 111001011100111 010101101011
    http://slipperyhill.co.uk - http://4theweb.co.uk

    Comment

    • Bill H

      #3
      Re: assigning variables in e.g. an if ....

      On Sep 25, 4:25 am, Geoff Berrow <blthe...@ckdog .co.ukwrote:
      Message-ID:
      <5f0aa7b8-0fe4-4b34-8f81-f288cb0ae...@59 g2000hsb.google groups.comfrom
      jodleren contained the following:
      >
      I have tried to use this, but failed.... and I have not found any
      proper information about this?
      >
      E.g., if I am right, then
      >
       if(strpos($line , '='))
      >
      could be
      >
       if( ($i=strpos($lin e, '='))  !== false )
         echo $i;
      >
      but just how does this work? the parantheses do it?
      I asume I have to understand that as a statement in a statement?
      >
      Not sure I understand your question.  AIUI parentheses work the same way
      they do in mathematics, ie the code inside is evaluated first.  The
      manual tells you what a function returns (note: some functions don't
      return anything more meaningful than true or false).  That value can be
      assigned to a variable.
      >
      Now the manual entry for strpos is this
      "Returns the position as an integer. If needle is not found, strpos()
      will return boolean FALSE." Note the warning, if the character being
      searched for is at the beginning of the string it could return 0, or in
      other words a non-boolean false.
      >
      So in the first example the 'if' will be executed if the character is
      found,as long as the character is not at position 0. To check for that
      you'd need
      >
       if(strpos($line , '=')!==false)
      >
      In the second example the bit in parentheses is evaluated first
      $i=strpos($line , '=')
      >
      $i now contains the output of strpos, an integer if the character is
      found, false if not.
      >
      So now the parentheses have been evaluated we effectively have:
      >
      if( $i  !== false )
       echo $i;
      >
      in other words $i will be echoed as long as it is not false.
      >
      --
      Geoff Berrow  011000100110110 0010000000110
      001101101011011 001000110111101 100111001011
      100110001101101 111001011100111 010101101011htt p://slipperyhill.co .uk-http://4theweb.co.uk
      It appears he is trying to do variable assignment inside the if ()
      statement. Is this allowed? I now in actionscript this is not allowed,
      and I believe in perl it is allowed and always returns true (could be
      wrong there).

      If so (or even if not) does a variable assignment return a true /
      false value?

      Bill H

      Comment

      • Gordon

        #4
        Re: assigning variables in e.g. an if ....

        On Sep 25, 10:30 am, Bill H <b...@ts1000.us wrote:
        On Sep 25, 4:25 am, Geoff Berrow <blthe...@ckdog .co.ukwrote:
        >
        >
        >
        Message-ID:
        <5f0aa7b8-0fe4-4b34-8f81-f288cb0ae...@59 g2000hsb.google groups.comfrom
        jodleren contained the following:
        >
        >I have tried to use this, but failed.... and I have not found any
        >proper information about this?
        >
        >E.g., if I am right, then
        >
         if(strpos($line , '='))
        >
        >could be
        >
         if( ($i=strpos($lin e, '='))  !== false )
           echo $i;
        >
        >but just how does this work? the parantheses do it?
        >I asume I have to understand that as a statement in a statement?
        >
        Not sure I understand your question.  AIUI parentheses work the same way
        they do in mathematics, ie the code inside is evaluated first.  The
        manual tells you what a function returns (note: some functions don't
        return anything more meaningful than true or false).  That value can be
        assigned to a variable.
        >
        Now the manual entry for strpos is this
        "Returns the position as an integer. If needle is not found, strpos()
        will return boolean FALSE." Note the warning, if the character being
        searched for is at the beginning of the string it could return 0, or in
        other words a non-boolean false.
        >
        So in the first example the 'if' will be executed if the character is
        found,as long as the character is not at position 0. To check for that
        you'd need
        >
         if(strpos($line , '=')!==false)
        >
        In the second example the bit in parentheses is evaluated first
        $i=strpos($line , '=')
        >
        $i now contains the output of strpos, an integer if the character is
        found, false if not.
        >
        So now the parentheses have been evaluated we effectively have:
        >
        if( $i  !== false )
         echo $i;
        >
        in other words $i will be echoed as long as it is not false.
        >
        --
        Geoff Berrow  011000100110110 0010000000110
        001101101011011 001000110111101 100111001011
        100110001101101 111001011100111 010101101011htt p://slipperyhill.co .uk-http://4theweb.co.uk
        >
        It appears he is trying to do variable assignment inside the if ()
        statement. Is this allowed? I now in actionscript this is not allowed,
        and I believe in perl it is allowed and always returns true (could be
        wrong there).
        >
        If so (or even if not) does a variable assignment return a true /
        false value?
        >
        Bill H
        As with most languages that have C as a comm,on ancestor, assigning
        within an IF is perfectly legal in PHP. I tend to use it a lot in
        places where calling functions that can fail. For example:

        if ($fileHandle = fopen ('/foo/bar/bax.txt'))
        {
        //...
        fclose ($fileHandle)
        }
        else
        {
        // Error handler goes here
        }

        Comment

        • Geoff Berrow

          #5
          Re: assigning variables in e.g. an if ....

          Message-ID:
          <10565497-9cda-4996-801e-ddf4811b0acb@e3 9g2000hsf.googl egroups.comfrom
          Gordon contained the following:
          >As with most languages that have C as a comm,on ancestor, assigning
          >within an IF is perfectly legal in PHP.
          And also very useful in while

          while($row=mysq l_fetch assoc($result)) {

          }
          --
          Geoff Berrow 011000100110110 0010000000110
          001101101011011 001000110111101 100111001011
          100110001101101 111001011100111 010101101011
          http://slipperyhill.co.uk - http://4theweb.co.uk

          Comment

          • mijn naam

            #6
            Re: assigning variables in e.g. an if ....

            "Bill H" <bill@ts1000.us schreef in bericht
            news:671de5e5-9460-4bec-ae97-793a4e6fbc9f@79 g2000hsk.google groups.com...

            But where does the "true" / "false" come from? Wouldn't a variable
            assignment always be "true", or does the true false come from the
            value of the variable after it has been assigned?

            Bill H



            <?php

            $a=10;
            $b=20;

            $a=$b=5;

            echo "a=$a\n";
            echo "b=$b\n";

            ?>

            Both variables are set to 5. $a is not set to true.


            Try this:

            <?php

            $a=0;

            if ($b=$a) echo "1: true\n";
            if (($b=$a)==TRUE) echo "2: true\n";
            if (($b=$a)===TRUE ) echo "3: true\n";

            if (!($b=$a)) echo "4: false\n";
            if (($b=$a)==FALSE ) echo "5: false\n";
            if (($b=$a)===FALS E) echo "6: false\n";

            $a=1;

            if ($b=$a) echo "7: true\n";
            if (($b=$a)==TRUE) echo "8: true\n";
            if (($b=$a)===TRUE ) echo "9: true\n";

            if (!($b=$a)) echo "10: false\n";
            if (($b=$a)==FALSE ) echo "11: false\n";
            if (($b=$a)===FALS E) echo "12: false\n";
            ?>

            4: false
            5: false
            7: true
            8: true

            Comment

            • Jerry Stuckle

              #7
              Re: assigning variables in e.g. an if ....

              jodleren wrote:
              Hi
              >
              I have been looking into php.net, but could not find any proper
              description.
              There is a way of assigning variables from functions, while at the
              same time using them in e.g. an if.
              I have tried to use this, but failed.... and I have not found any
              proper information about this?
              >
              E.g., if I am right, then
              >
              if(strpos($line , '='))
              >
              could be
              >
              if( ($i=strpos($lin e, '=')) !== false )
              echo $i;
              >
              but just how does this work? the parantheses do it?
              I asume I have to understand that as a statement in a statement?
              >
              WBR
              Sonnich
              >
              >
              Sure, it works fine. The secret is that PHP, like C, C++ and Java,
              treat the assignment operation ("=") as an operator. That is, $a=$b is
              an expression, just as $a+$b is, and returns a value. In the case of
              $a+$b, the returned value is, of course, the sum of $a and $b. For
              $a=$b, it is the value of $a after the assignment. It means that an
              assignment expression can be used anywhere another expression can be
              used, i.e.

              func($c=$a+$b).

              The only thing to be cautious about is that the "=" operator has very
              low precedence, so it's best to always enclose an expression using it in
              parens to prevent unexpected results (as you did in your example).

              P.S. A statement is one or more expressions terminated by a semicolon.

              $i=strpos($line , '=') // is an expression.

              $i=strpos($line , '='); // is a statement.

              A minor syntactical difference, but a major difference in operation.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • =?UTF-8?B?SXbDoW4gU8OhbmNoZXogT3J0ZWdh?=

                #8
                Re: assigning variables in e.g. an if ....

                Bill H wrote:
                But where does the "true" / "false" come from? Wouldn't a variable
                assignment always be "true", or does the true false come from the
                value of the variable after it has been assigned?
                The result of an assignment operation is the assigned value, which then gets
                implicitly casted to a boolean value (because the 'if' construct expects a
                boolean value).

                --
                ----------------------------------
                Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

                Error 99: - CPU too tired to continue.

                Comment

                Working...