首页 > 代码库 > Joining strings
Joining strings
When working with variables that contain strings or numbers. There‘s often a need to join their values. Either to display directly on screen or to store in a variable. This is particularly important when using PHP to send an email message. There are several ways to build strings from variables. The basic method is to use the concatenation operator. Concatenation is just a fancy technical term that means to link in a chain or series. In many programming languages, the plus sign is used as the concatenation operator.
But PHP is the odd man out. It uses a dot or period. Now, in this file here, I‘ve got several variables that I‘ve prepared in advance. And what I want to do is to concatenate together the values of first name and last name and store that as full name. So, first name, then I‘ll add a space, the concatenation operator and last name. And then, to be able to display it, we will echo full name. Save the page and view it in the browser.
Now, even though there‘s a space between the first name and last name around the concatenation operator, there‘s no space between the two values. Because as I‘ve explained on several occasions, PHP ignores white space within it‘s, in code. So to add a space between the two names, I need to go back into the code, and I‘ll add a literal string which consists of a single space, and the concatenation operator.
Save it and reload the page. And there we have the space in-between. Now, the reason I‘ve put a space around the concatenation operator is because it‘s quite small and can be difficult to see. I‘ve put the space there purely and simply for readability. If you don‘t want the space there, then of course, you can leave it out. Another way to join strings is to use a double-quoted string. So, let‘s create a variable called recommendation and a double-quoted string.
We‘ll add book and the literal word, buy. And then Author, and to display it again, we‘ll use echo. Now, because PHP ignores white space, I‘ll need to add an HTML br tag here as a literal string.
So, we have the concatenation operator followed by a literal string containing the HTML br tag. Save that and refresh the browser. So, both lines are now being displayed. And of course, the br tag isn‘t displayed, because that‘s being used by the browser purely and simple to create that new line. When sending an email with PHP, the body of the message needs to be a single string.
Often, this information comes from many different fields in an online forum. So the most convenient way to merge them into a single string is with a combined concatenation operator. This consists of a period or dot immediately followed by an equal sign. So let‘s say I‘m going to send all of these variable as part of an email message. So I‘ll call the message. Really, original name, message, and then just ordinary equals sign, and will have a double-quoted string name.
And then full name, and I want a line, a blank line to appear between each part of the email message, so I‘ve created a variable here called new lines, which is in a double-quoted string, and it has the escape characters rn, rn. This is the carriage return followed by a new line character and another carriage return and new line character. So we‘ll add that here, and that‘s the first part of the message. I want to add something to that message, so this time I use the combine concatenation operator, a period followed by an equal sign with no space in-between, and then, the next part of the message, and again, the new line characters.
And add more to the message again with the combined concatenation operator times listened and that‘s the end of the message. I don‘t need any more new lines, so I‘ll just close the double quotes there and put the semicolon at the end, and we need to echo that.
And to prevent the earlier values being displayed, I‘ll just comment those out. Save page and Reload the browser. Now you can‘t see those new lines here, but that‘s because browsers ignore new line characters. So if we go in to view source, uou can see that there is a blank line between each part of the message. And that all the values have been added together as a single message. So, each part is added to the previous part when you use the combined concatenation operator.
There‘s no limit to the length of a string, but don‘t go mad. Some beginners think that everything in a PHP page needs to be stored in variables and displayed using echo, and that‘s not true. PHP is designed to be embedded in HTML, so only those parts that change need to be stored as variables. I‘ve got an example here which use the same variables as before, and I‘ve embedded everything in ordinary HTML. In this case, the variables are hardcoded into the top of the page, but in an ordinary website, normally, they would come from a database or from some other external source. So the static text is in HTML.
And only those parts that are going to change use PHP and variables. And notice here, that I‘ve used an opening angled bracket, question mark and equal sign, rather than the full PHP opening tag and echo. This is a shortened way of displaying a single variable or string that‘s embedded within HTML. On servers running PHP 5.3 or earlier, you need to test this, because it relies on short open text being enabled in php.ini.
In PHP 5.4, which was at an advanced stage of development when I recorded this video, the shorthand no longer relies on short open text and is always enabled. So let‘s just view this in a browser. And you can see that the values in the variables have been inserted into the static HTML. So, when you need to join strings use the concatenation operator, the double quotes, or the combine catenation operator, but when there‘s a lot of static text, don‘t forget that PHP can be embedded into HTML.
Joining strings