SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need some explaining on the cat operator/concatenation pls

    Hello

    Thanks for the forum as Im new and trying to get my hands around learning php...

    My knowledge is: html, css, joomla web design...

    My question: I dont seem to understand how the cat operator works, I understand it "binds" a string together, but I dont understand (yet) how the code is actually written and where the operators are placed, namely . and " ".I understand what it does , but I dont seem to know how to actually write the code properly e.g take this:
    $first = "Bill";
    $last = "McCarthy";
    $name = $first . " " . $last;Why are these inverted commas in the middle and two concatenation operators, what is it in place of?

    Can someone PLEEEEASE explain? What is supposed to go where so I can finally get my head around writing strings with the cat operator

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there.

    I found concatenation confusing at first too. Hopefully I can explain it in a way that's easy to understand.

    Why are these inverted commas in the middle and two concatenation operators, what is it in place of?
    The quotes are there specifically to put a space between the first and last name. If that wasn't there it would read BillMcCarthy.

    Consider this text string with PHP variables:

    PHP Code:
    $string $greeting ", user, how are you " $timeOfDay "?"
    That statement, depending on the values of the variables would read: "Salutations, user, how are you tonight?"

    Basically, everything that's hardcoded or static text needs to be between quotes. When you want to insert a variable in a string, you have to append it to the string with a period. Before you concat, you need to break from the string (or quotes).

    EDIT: to clarify a bit more, say you wanted to add a middlename to the example you have in your post but you want it hardcoded in:

    PHP Code:
    $first "Bill";
    $last "McCarthy";
    $name $first " Donald " $last
    This would read: Bill Donald McCarthy.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that makes a lot of sense now and much easier to understand, thanks its great that there are people like like you willing to help xxx

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's my pleasure! I owe a lot to the fine people who give advice in programming forums. The least I can do is give back now that I am able to.

  5. #5
    SitePoint Enthusiast webdesignhouston's Avatar
    Join Date
    Dec 2010
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Skye1 View Post
    $first = "Bill";
    $last = "McCarthy";
    $name = $first . " " . $last;Why are these inverted commas in the middle and two concatenation operators, what is it in place of?

    Can someone PLEEEEASE explain? What is supposed to go where so I can finally get my head around writing strings with the cat operator
    In your strings you have two concatenation operators and three operands -- one string literal and two variables. PHP will concatenate strings one by one. The " " is just a string, a space actually which you add between two variables so that they are separated by a space and will be read as:

    Bill McCarthy

    You can concatenate as many operands as you like in one go. PHP concatenates them left to right and stores the final result in $name. Its like adding three or more numbers like $name = 1 + 2 + 3 (+ 4 + ...)


Bookmarks

Posting Permissions

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