Possibile bug?

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

    Possibile bug?


    function foo() { return 'Hello World!'; }
    if ( $str = foo() || $str = foo() ) echo $str;

    What should output this piece of code?
    I think it's not normal what's outputting to me...

    Let me know. :-)
    Maverick

    --
    «Che non siete più liberi di non avere una casa con letto per dormire.
    Lavorate voi - schiavi dei soldi - che non sentite la puzza della
    schiavitù!» (a Mario di Monte Mario, Roma) # http://libre.netsons.org/
  • Maverick FC

    #2
    Re: Possibile bug?

    On Fri, 23 Mar 2007 01:57:27 GMT, "Maverick FC"
    <freeculture@NO SPAMlibero.itwr ote:
    What should output this piece of code?
    I think it's not normal what's outputting to me...
    Here it is what I'm trying to say:

    function foo() { return 'Hello World!'; }

    echo '<pre>';
    if ( $str = foo() ) var_dump($str);
    if ( $str = foo() || $str = foo() ) var_dump($str);
    echo '</pre>';

    Is that normal?

    Thanks.
    Maverick

    --
    «Che non siete più liberi di non avere una casa con letto per dormire.
    Lavorate voi - schiavi dei soldi - che non sentite la puzza della
    schiavitù!» (a Mario di Monte Mario, Roma) # http://libre.netsons.org/

    Comment

    • Geoff Muldoon

      #3
      Re: Possibile bug?

      freeculture@NOS PAMlibero.it says...
      >
      function foo() { return 'Hello World!'; }
      if ( $str = foo() || $str = foo() ) echo $str;
      >
      What should output this piece of code?
      I think it's not normal what's outputting to me...
      >
      Let me know. :-)
      Maverick
      It should (and I presume does) return 1.

      I presume you just don't know why.

      Geoff M

      Comment

      • Maverick FC

        #4
        Re: Possibile bug?

        On Fri, 23 Mar 2007 13:52:14 +1100, Geoff Muldoon
        <geoff.muldoon@ trap.gmail.comw rote:
        It should (and I presume does) return 1.
        Actually, it returns bool(true).
        I presume you just don't know why.
        You're correct. :-) Why?

        This code:

        if ( $str = foo() ) var_dump($str);

        returns the string - as I expected.

        foo() is always - correctly - executed only once.
        I just can't understand this behaviour.

        Thanks.
        Maverick

        --
        «Che non siete più liberi di non avere una casa con letto per dormire.
        Lavorate voi - schiavi dei soldi - che non sentite la puzza della
        schiavitù!» (a Mario di Monte Mario, Roma) # http://libre.netsons.org/

        Comment

        • Maverick FC

          #5
          Re: Possibile bug?

          On Fri, 23 Mar 2007 02:32:12 GMT, "Maverick FC"
          <freeculture@NO SPAMlibero.itwr ote:
          if ( $str = foo() || $str = foo() ) var_dump($str);
          Ok, I got it.
          I simply have to use brackets:

          if ( ($str = foo()) || ($str = foo()) ) var_dump($str);

          Stupid precedence! ;-)

          Thank you, Geoff.

          Greetings.
          Maverick

          --
          «Che non siete più liberi di non avere una casa con letto per dormire.
          Lavorate voi - schiavi dei soldi - che non sentite la puzza della
          schiavitù!» (a Mario di Monte Mario, Roma) # http://libre.netsons.org/

          Comment

          • peter

            #6
            Re: Possibile bug?

            >It should (and I presume does) return 1.
            >
            Actually, it returns bool(true).
            >
            >I presume you just don't know why.
            >
            You're correct. :-) Why?
            >
            This code:
            >
            if ( $str = foo() ) var_dump($str);
            >
            returns the string - as I expected.
            >
            foo() is always - correctly - executed only once.
            I just can't understand this behaviour.
            $str = foo() is always going to be true as you are assigning $str to foo()
            if you are trying to see if what is returned by foo() is the same as $str
            then you should use == or ===


            Comment

            • Geoff Berrow

              #7
              Re: Possibile bug?

              Message-ID: <etvt90$fgf$1@a ioe.orgfrom peter contained the following:
              >$str = foo() is always going to be true as you are assigning $str to foo()
              >if you are trying to see if what is returned by foo() is the same as $str
              >then you should use == or ===
              Spoilsport...

              --
              Geoff Berrow (put thecat out to email)
              It's only Usenet, no one dies.
              My opinions, not the committee's, mine.
              Simple RFDs http://www.ckdog.co.uk/rfdmaker/

              Comment

              • David Quinton

                #8
                Re: Possibile bug?

                On Fri, 23 Mar 2007 01:57:27 GMT, "Maverick FC"
                <freeculture@NO SPAMlibero.itwr ote:
                >
                >function foo() { return 'Hello World!'; }
                >if ( $str = foo() || $str = foo() ) echo $str;
                >
                >What should output this piece of code?
                >I think it's not normal what's outputting to me...
                == for comparison
                = for assignment
                --
                Locate your Mobile phone: <http://www.bizorg.co.u k/news.html>
                Great gifts: <http://www.ThisBritain .com/ASOS_popup.html >

                Comment

                • peter

                  #9
                  Re: Possibile bug?

                  Spoilsport...
                  lol sorry did I spoil your fun


                  Comment

                  Working...