array bugs?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 7h@ch

    #1

    array bugs?


    Sorry if I sounds like a noob (because I am one, here). I ran across
    this earlier, and it kept bugging me. I guess someone here know a good
    answer.

    To summerize, I was using array within a class. After some
    manipulation, the value of the array's element that I needed wasn't
    "echoed" right.

    For instance,

    class FootballClub
    {
    public $name;
    public $stadium = "junkyard";
    public $roster = array("gk"=>"cl own", "st"=>"fool ");
    public $coach;
    }

    $myclub = new FootballClub();
    $myclub->name = "Liverpool" ;

    if ($myclub->name == "Liverpool" )
    {
    $myclub->stadium = "Anfield";
    $myclub->coach = "Rafa Benitez";
    $myclub->roster["gk"] = "Pepe Reina";
    $myclub->roster["st"] = "Robbie Fowler";
    }


    $myplayer = $myclub->roster[gk];
    echo "your team plays at: $myclub->stadium<br>" ;
    echo "your goal keeper is: $myclub->roster[gk]<br>");

    ---------
    Problem:

    As is, the 2nd line output is: "your goal keeper is: Array[gk]" thought
    $myplayer gets the right value.

    Any plausible explaination, please?

    Thanks.

  • Miguel Cruz

    #2
    Re: array bugs?

    "7h@ch" <essetm@gmail.c omwrote:
    echo "your goal keeper is: $myclub->roster[gk]<br>");
    >
    As is, the 2nd line output is: "your goal keeper is: Array[gk]"
    echo "your goal keeper is: {$myclub->roster['gk']}<br>");

    Note the curly braces and quotes.

    miguel
    --
    Photos from 40 countries on 5 continents: http://travel.u.nu
    Latest photos: Malaysia; Thailand; Singapore; Spain; Morocco
    Airports of the world: http://airport.u.nu

    Comment

    • Rik

      #3
      Re: array bugs?

      Miguel Cruz wrote:
      "7h@ch" <essetm@gmail.c omwrote:
      >echo "your goal keeper is: $myclub->roster[gk]<br>");
      >>
      >As is, the 2nd line output is: "your goal keeper is: Array[gk]"
      >
      echo "your goal keeper is: {$myclub->roster['gk']}<br>");
      >
      Note the curly braces and quotes.
      Yup, see Complex (curly) syntax:


      Grtz,
      --
      Rik Wasmus


      Comment

      • s a n j a y

        #4
        Re: array bugs?

        7h@ch wrote:
        Sorry if I sounds like a noob (because I am one, here). I ran across
        this earlier, and it kept bugging me. I guess someone here know a good
        answer.
        >
        To summerize, I was using array within a class. After some
        manipulation, the value of the array's element that I needed wasn't
        "echoed" right.
        >
        For instance,
        >
        class FootballClub
        {
        public $name;
        public $stadium = "junkyard";
        public $roster = array("gk"=>"cl own", "st"=>"fool ");
        public $coach;
        }
        >
        $myclub = new FootballClub();
        $myclub->name = "Liverpool" ;
        >
        if ($myclub->name == "Liverpool" )
        {
        $myclub->stadium = "Anfield";
        $myclub->coach = "Rafa Benitez";
        $myclub->roster["gk"] = "Pepe Reina";
        $myclub->roster["st"] = "Robbie Fowler";
        }
        >
        >
        $myplayer = $myclub->roster[gk];
        echo "your team plays at: $myclub->stadium<br>" ;
        echo "your goal keeper is: $myclub->roster[gk]<br>");
        >
        ---------
        Problem:
        >
        As is, the 2nd line output is: "your goal keeper is: Array[gk]" thought
        $myplayer gets the right value.
        >
        Any plausible explaination, please?
        >
        Thanks.
        >
        Although not strictly necessary in php, always put your variables
        outside of quotes. This came to me easily as I was developing in c and
        C++ before I moved on to PHP.

        Comment

        • Shelly

          #5
          Re: array bugs?


          "s a n j a y" <sanjay.debian@ gmail.comwrote in message
          news:NaCdnUAhAv Wsg0vZnZ2dnUVZ_ qydnZ2d@comcast .com...
          7h@ch wrote:
          >Sorry if I sounds like a noob (because I am one, here). I ran across
          >this earlier, and it kept bugging me. I guess someone here know a good
          >answer.
          >>
          >To summerize, I was using array within a class. After some
          >manipulation , the value of the array's element that I needed wasn't
          >"echoed" right.
          >>
          >For instance,
          >>
          >class FootballClub
          >{
          >public $name;
          >public $stadium = "junkyard";
          >public $roster = array("gk"=>"cl own", "st"=>"fool ");
          >public $coach;
          >}
          >>
          >$myclub = new FootballClub();
          >$myclub->name = "Liverpool" ;
          >>
          >if ($myclub->name == "Liverpool" )
          >{
          >$myclub->stadium = "Anfield";
          >$myclub->coach = "Rafa Benitez";
          >$myclub->roster["gk"] = "Pepe Reina";
          >$myclub->roster["st"] = "Robbie Fowler";
          >}
          >>
          >>
          >$myplayer = $myclub->roster[gk];
          >echo "your team plays at: $myclub->stadium<br>" ;
          >echo "your goal keeper is: $myclub->roster[gk]<br>");
          >>
          > ---------
          > Problem:
          >>
          >As is, the 2nd line output is: "your goal keeper is: Array[gk]" thought
          >$myplayer gets the right value.
          >>
          >Any plausible explaination, please?
          >>
          >Thanks.
          >>
          >
          Although not strictly necessary in php, always put your variables outside
          of quotes. This came to me easily as I was developing in c and C++ before
          I moved on to PHP.
          Hmm. I looke at someone else's answer of putting braces in. I thought to
          myself, "Great, I just learned something by reading this newgroup. Store it
          away". When I saw your response I thought to myself "Why hadn't I come
          across this problem before, myself?". Well, your answer showed me why. I
          would have written it as:

          echo "your goal keeper is:" . $myclub->roster['gk'] . "<br>";

          and never have had that difficulty. My background in C and java developed
          that same habit in me.

          Shelly


          Comment

          • Rik

            #6
            Re: array bugs?

            Shelly wrote:
            "s a n j a y" <sanjay.debian@ gmail.comwrote in message
            news:NaCdnUAhAv Wsg0vZnZ2dnUVZ_ qydnZ2d@comcast .com...
            >7h@ch wrote:
            >>Sorry if I sounds like a noob (because I am one, here). I ran across
            >>this earlier, and it kept bugging me. I guess someone here know a
            >>good answer.
            >>>
            >>To summerize, I was using array within a class. After some
            >>manipulatio n, the value of the array's element that I needed wasn't
            >>"echoed" right.
            >>>
            >>For instance,
            >>>
            >>class FootballClub
            >>{
            >>public $name;
            >>public $stadium = "junkyard";
            >>public $roster = array("gk"=>"cl own", "st"=>"fool ");
            >>public $coach;
            >>}
            >>>
            >>$myclub = new FootballClub();
            >>$myclub->name = "Liverpool" ;
            >>>
            >>if ($myclub->name == "Liverpool" )
            >>{
            >>$myclub->stadium = "Anfield";
            >>$myclub->coach = "Rafa Benitez";
            >>$myclub->roster["gk"] = "Pepe Reina";
            >>$myclub->roster["st"] = "Robbie Fowler";
            >>}
            >>>
            >>>
            >>$myplayer = $myclub->roster[gk];
            >>echo "your team plays at: $myclub->stadium<br>" ;
            >>echo "your goal keeper is: $myclub->roster[gk]<br>");
            >>>
            >> ---------
            >> Problem:
            >>>
            >>As is, the 2nd line output is: "your goal keeper is: Array[gk]"
            >>thought $myplayer gets the right value.
            >>>
            >>Any plausible explaination, please?
            >>>
            >>Thanks.
            >>>
            >>
            >Although not strictly necessary in php, always put your variables
            >outside of quotes. This came to me easily as I was developing in c
            >and C++ before I moved on to PHP.
            >
            Hmm. I looke at someone else's answer of putting braces in. I
            thought to myself, "Great, I just learned something by reading this
            newgroup. Store it away". When I saw your response I thought to
            myself "Why hadn't I come across this problem before, myself?".
            Well, your answer showed me why. I would have written it as:
            >
            echo "your goal keeper is:" . $myclub->roster['gk'] . "<br>";
            >
            and never have had that difficulty. My background in C and java
            developed that same habit in me.
            It could be done like that, for sure (allthough I'd use single quotes for
            text without variables & newlines). When outputting HTML tags however, it
            can become a complete quote-fest, that I'd rather avoid to improve
            legibility. Heredoc & curly often come to the rescue, allthough certainly
            for numberformattin g, but also for strings, (s/v)printf() is certainly an
            outcome.

            Consider:
            array product{
            [id] =>.....
            [name] =>...
            [decription] =...
            [image_src] =...
            .....
            }

            Concating way:

            echo '
            <h2>'.$produc t['id'].':'.$product['name'].'</h2>
            <p>
            <a href="./products/?id='.$product['id'].'"><img
            src="'.$product['image_src'].'" /></a>
            '.$product['decscription'].'
            let\'s say way have to use singel quote\'s here... escaping?
            Read more about <a
            href="./products/?id='.$product['id'].'">'.$produc t['name'].'</a>
            </p>';

            Double quotes, curly:

            echo "
            <h2>{$product['id']}:{$product['name']}</h2>
            <p>
            <a href=\"./products/?id={$product['id']}\"><img
            src=\"{$product['image_src']}\" /></a>
            {$product['decscription']}
            let's say way have to use singel quote's here... escaping?
            Read more about <a
            href=\"./products/?id={$product['id']}\">{$product['name']}</a>
            </p>';

            Heredoc, curly

            echo <<<HTML
            <h2>{$product['id']}:{$product['name']}</h2>
            <p>
            <a href="./products/?id={$product['id']}"><img
            src="{$product['image_src']}" /></a>
            {$product['decscription']}
            let's say way have to use singel quote's here... escaping?
            Read more about <a
            href="./products/?id={$product['id']}">{$product['name']}</a>
            </p>
            HTML;

            Printf possibility:

            vprintf(<<<HTML
            <h2>%1$03d:%2$s </h2>
            <p>
            <a href="./products/?id=%1$d"><img src="%4$s" /></a>
            %3$s
            let's say way have to use single quote's here... escaping?
            Read more about <a href="./products/?id=%1$d">%2$s</a>
            </p>
            HTML;
            , $products);



            The latter one might come in handywhen outputting a list from an array
            (allthough the code isn't really charming):

            $list = array('page 1','page 2','page 3'....
            vprintf(str_rep eat("\n<li>%s</li>", count($list)),$ list);

            It's all about legibility, what you're actually doing, wether it has to be
            easily maintained and wether it's a repeating output.

            Grtz,
            --
            Rik Wasmus


            Comment

            • Shelly

              #7
              Re: array bugs?


              "Rik" <luiheidsgoeroe @hotmail.comwro te in message
              news:6dc6b$44d6 4f53$8259c69c$2 4012@news1.tude lft.nl...
              Shelly wrote:
              >"s a n j a y" <sanjay.debian@ gmail.comwrote in message
              >news:NaCdnUAhA vWsg0vZnZ2dnUVZ _qydnZ2d@comcas t.com...
              >>7h@ch wrote:
              >>>Sorry if I sounds like a noob (because I am one, here). I ran across
              >>>this earlier, and it kept bugging me. I guess someone here know a
              >>>good answer.
              >>>>
              >>>To summerize, I was using array within a class. After some
              >>>manipulation , the value of the array's element that I needed wasn't
              >>>"echoed" right.
              >>>>
              >>>For instance,
              >>>>
              >>>class FootballClub
              >>>{
              >>>public $name;
              >>>public $stadium = "junkyard";
              >>>public $roster = array("gk"=>"cl own", "st"=>"fool ");
              >>>public $coach;
              >>>}
              >>>>
              >>>$myclub = new FootballClub();
              >>>$myclub->name = "Liverpool" ;
              >>>>
              >>>if ($myclub->name == "Liverpool" )
              >>>{
              >>>$myclub->stadium = "Anfield";
              >>>$myclub->coach = "Rafa Benitez";
              >>>$myclub->roster["gk"] = "Pepe Reina";
              >>>$myclub->roster["st"] = "Robbie Fowler";
              >>>}
              >>>>
              >>>>
              >>>$myplayer = $myclub->roster[gk];
              >>>echo "your team plays at: $myclub->stadium<br>" ;
              >>>echo "your goal keeper is: $myclub->roster[gk]<br>");
              >>>>
              >>> ---------
              >>> Problem:
              >>>>
              >>>As is, the 2nd line output is: "your goal keeper is: Array[gk]"
              >>>thought $myplayer gets the right value.
              >>>>
              >>>Any plausible explaination, please?
              >>>>
              >>>Thanks.
              >>>>
              >>>
              >>Although not strictly necessary in php, always put your variables
              >>outside of quotes. This came to me easily as I was developing in c
              >>and C++ before I moved on to PHP.
              >>
              >Hmm. I looke at someone else's answer of putting braces in. I
              >thought to myself, "Great, I just learned something by reading this
              >newgroup. Store it away". When I saw your response I thought to
              >myself "Why hadn't I come across this problem before, myself?".
              >Well, your answer showed me why. I would have written it as:
              >>
              >echo "your goal keeper is:" . $myclub->roster['gk'] . "<br>";
              >>
              >and never have had that difficulty. My background in C and java
              >developed that same habit in me.
              >
              It could be done like that, for sure (allthough I'd use single quotes for
              text without variables & newlines). When outputting HTML tags however, it
              can become a complete quote-fest, that I'd rather avoid to improve
              legibility. Heredoc & curly often come to the rescue, allthough certainly
              for numberformattin g, but also for strings, (s/v)printf() is certainly an
              outcome.
              Sometimes I use single and sometimes double if there is no necessity to
              choose between them. See below.
              >
              Consider:
              array product{
              [id] =>.....
              [name] =>...
              [decription] =...
              [image_src] =...
              .....
              }
              >
              Concating way:
              >
              echo '
              <h2>'.$produc t['id'].':'.$product['name'].'</h2>
              <p>
              <a href="./products/?id='.$product['id'].'"><img
              src="'.$product['image_src'].'" /></a>
              '.$product['decscription'].'
              let\'s say way have to use singel quote\'s here... escaping?
              Read more about <a
              href="./products/?id='.$product['id'].'">'.$produc t['name'].'</a>
              </p>';
              Here I would do the preceding without needing escapes:

              echo ' <h2>' . $product['id'] . ':' .$product['name'] .
              '</h2><p><a href="./products/?id=' . $product['id'] . '" ><img src="' .
              $product['image_src'] . '" /></a>' . $product['decscription'] .
              "let's say way have to use single quote's here... escaping?Read more
              about <a " .
              'href="/products/?id=' . $product['id'] . '">' . $product['name'] .
              '</a></p>';

              Putting in spaces between the concatonation dots greatly improves
              legibility. Also, not needing the extra escape character (when it can be
              avoided) also does that.
              >
              Double quotes, curly:
              >
              echo "
              <h2>{$product['id']}:{$product['name']}</h2>
              <p>
              <a href=\"./products/?id={$product['id']}\"><img
              src=\"{$product['image_src']}\" /></a>
              {$product['decscription']}
              let's say way have to use singel quote's here... escaping?
              Read more about <a
              href=\"./products/?id={$product['id']}\">{$product['name']}</a>
              </p>';
              >
              Heredoc, curly
              >
              echo <<<HTML
              <h2>{$product['id']}:{$product['name']}</h2>
              <p>
              <a href="./products/?id={$product['id']}"><img
              src="{$product['image_src']}" /></a>
              {$product['decscription']}
              let's say way have to use singel quote's here... escaping?
              Read more about <a
              href="./products/?id={$product['id']}">{$product['name']}</a>
              </p>
              HTML;
              >
              Printf possibility:
              >
              vprintf(<<<HTML
              <h2>%1$03d:%2$s </h2>
              <p>
              <a href="./products/?id=%1$d"><img src="%4$s" /></a>
              %3$s
              let's say way have to use single quote's here... escaping?
              Read more about <a href="./products/?id=%1$d">%2$s</a>
              </p>
              HTML;
              , $products);
              >
              >
              >
              The latter one might come in handywhen outputting a list from an array
              (allthough the code isn't really charming):
              >
              $list = array('page 1','page 2','page 3'....
              vprintf(str_rep eat("\n<li>%s</li>", count($list)),$ list);
              >
              It's all about legibility, what you're actually doing, wether it has to be
              easily maintained and wether it's a repeating output.
              I agree with this last statement 100%. That is why I always put in a spaces
              where possible and end the first line with the concatonation dot to and
              start the second line in the right indent position.

              Shelly


              Comment

              • 7h@ch

                #8
                Re: array bugs?


                Thanks all.

                Guess I gotta take out some of the sloppy coding habits ;-)

                Comment

                Working...