Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2010
    Location
    USA, NJ
    Posts
    42
    Thanks
    16
    Thanked 0 Times in 0 Posts

    for_loop and concatenation assignment operator

    Can someone please explain why this piece of code only outputs the last item in the array when I use the assignment operator and outputs all the items in the array when I use the concatenation assignment operator. And do I need to initialize the variable $result as shown in the code?


    PHP Code:

    <?php

    $array 
    = array("NJ","NY","FL","NH","VT","CA");

    function 
    test_1($array)    {
        
    $result "";
    for(
    $i=0;$i<count($array);$i++){
        
    $result .= "Element $i contains $array[$i]<br/>";
    }
    return 
    $result;
    }
    echo 
    test_1($array);
    ?>
    output with assignment operator.

    Element 5 contains CA

    output with concatenation assignment operator.

    Element 0 contains NJ
    Element 1 contains NY
    Element 2 contains FL
    Element 3 contains NH
    Element 4 contains VT
    Element 5 contains CA

  • #2
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    when you use the assignment operator you are essentially overwriting $result for every iteration.

    by using the concat assignment you build the string, appending each iterated result to the last, hence you see all of them.

    you don't strictly need to initialise the $result var, and you'd be better off doing:

    PHP Code:
    var $result
    to initalise it, rather than

    PHP Code:
    $result=''

  • Users who have thanked met for this post:

    steadythecourse (07-09-2010)

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Uh... it outputs the last row when you assign it because that's what you're doing... assigning the line each time it goes through the loop. Any data previous stored is overwritten. It concatenates when you use the .= because... thats what concatenation means, to add on to the end...

    And initializing, specially when doing concatenation, is usually a good idea; it helps to make sure if the variable was used previously that its refreshed. Of course, if you haven't used it previously, you can just initialize the variable with

    PHP Code:
    var $result

  • Users who have thanked Keleth for this post:

    steadythecourse (07-09-2010)

  • #4
    New Coder
    Join Date
    Mar 2010
    Location
    USA, NJ
    Posts
    42
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Thank You,
    Sometimes I feel foolish when the answer is so simple.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •