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.

时间: 2024-10-31 00:08:32

Joining strings的相关文章

ECMAScript6 -Symbols

Symbols-标志 ECMAScript 6 symbols began as a way to create private object members, a feature JavaScript developers have long wanted. The focus was around creating properties that were not identified by string names. Any property with a string name was

PHP Strings

<?php //1. how to use mail function //create short variable names $name=$_POST['name']; $email=$_POST['email']; $feedback=$_POST['feedback']; //set up some static information //$toaddress = "[email protected]"; $toaddress = "[email prote

43. Multiply Strings

1. 问题描述 Given two numbers represented as strings, return multiplication of the numbers as a string.Note:The numbers can be arbitrarily large and are non-negative.Converting the input string to integer is NOT allowed.You should NOT use internal librar

Multiply Strings

package cn.edu.xidian.sselab;/** * title:Multiply Strings * content: * Given two numbers represented as strings, return multiplication of the numbers as a string. * Note: The numbers can be arbitrarily large and are non-negative. * 读已知条件可以,两个String转换

leetcode笔记:Isomorphic Strings

一.题目描述 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of chara

LeetCode:Isomorphic Strings

1.题目名称 Isomorphic Strings(同构的字符串) 2.题目地址 https://leetcode.com/problems/isomorphic-strings/ 3.题目内容 英文: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurre

poj 2406 Power Strings KMP

Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative inte

go语言学习 strings常用函数

strings包中的函数用法 参考链接http://studygolang.com/articles/88 1.strings.replace() 函数原型 func Replace(str1, old, str2, n int) string //old是str1中的字符串,用str2替换str1中的old,一共替换n个.如果n<0,则全部替换 fmt.Println(strings.Replace("tet tet tet", "e", "es&

strings用法小记

By francis_hao    Feb 14,2017 打印文件中可打印字符,每个序列至少四(可配置)个字符长.主要用于显示非文本文件 概述 选项解释 -a --all - 扫描整个文件,不管那些段是否被加载或初始化,一般此项是默认的,除非程序被配置成-d的模式 -d --data 只打印初始化过或加载过的部分,此项可以减少一些垃圾数据. -f --print-file-name 在打印字符之前打印文件名 -min-len -n min-len --bytes=min-len 指定打印的最短