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
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post

    Newbie concatenation vs append question

    hi.

    i'm learning PHP. Early stages.

    Can somebody tell me why the simple concatenation period "." doesn't work in this example?

    <?php
    $firstString = "The quick box fox";
    $secondString = " jumped over the lazy dog.";
    $thirdString = $firstString;
    $thirdString .= $secondString;
    echo $thirdString;
    ?>


    The above code works and outputs "The quick box fox jumped over the lazy dog."

    BUT! If I remove the equal-sign so that the code in the next-to-last line looks like this:

    $thirdString . $secondString;

    I only get outputted > "The quick box fox"

    Why no "jumped over the lazy dog."?

    What happened to the value for the $secondString?

    Doesn't concatenate put the two together?

    Or does it work only for strings and not variables?
    Last edited by PonchoX; 02-02-2010 at 01:05 AM.

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    If you remove the assignment operator, there's no assignment. The variable $thirdString doesn't add the $secondString variable value. It may be a valid expression in that it doesn't cause the interpreter to fail, but it doesn't "do" anything. Now, if you were to do this:

    PHP Code:
    $firstString "The quick box fox";
    $secondString " jumped over the lazy dog.";
    $thirdString $firstString;

    echo 
    $thirdString $secondString
    That concatenation operator has some effect.

  • Users who have thanked bdl for this post:

    PonchoX (02-02-2010)

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    BUT! If I remove the equal-sign so that the code in the next-to-last line looks like this:

    $thirdString . $secondString;

    I only get outputted > "The quick box fox"

    Why no "jumped over the lazy dog."?

    What happened to the value for the $secondString?
    . is just doing the concatenation bu the statement
    Code:
     $thirdString .= $secondString;
    is equivalent to
    Code:
     $thirdString =  $thirdString.$secondString;
    .

    Thus when you remove the =, it reads like
    Code:
     $thirdString . $secondString;
    and thus it's not assigning anything to the variable $thirdString.

    However the following statement will show you the same output.
    Code:
     echo $thirdString . $secondString;
    (though it won't modify the value of $thirdString)
    Last edited by abduraooft; 02-01-2010 at 08:19 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    PonchoX (02-02-2010)

  • #4
    New Coder
    Join Date
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post
    okay, i see.

    the concatenation is not permanent, but only valid for that one statement, which i didn't echo.

    the last statement, which i DID echo did not pick up the $secondString value cuz it hadn't become permanently attached to $firstString by the concatenation operator.

    yes?


  •  

    Posting Permissions

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