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:blush:

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:


$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:


$first = "Bill";
$last = "McCarthy";
$name = $first . " Donald " . $last;

This would read: Bill Donald McCarthy.

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

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.

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 + …)