首页 > 代码库 > Storing text as strings

Storing text as strings

In common with other programming languages, PHP uses the term string to refer to text. When you store a string in a variable, you need to enclose it in quotes. Both single and double quotes are acceptable, but they work in subtly different ways. First, let‘s create a variable to store the title of one of my favorite books. I‘m going to use single quotes. Now, as soon as I type the ‘s, my editing program tells me there‘s a syntax error on that line.

I‘m going to ignore it for a moment and continue with the title. The problem is that quotes must be in matching pairs, and PHP sees the apostrophe in hitchhikers as being the matching closing quote this opening quotes here. So, all the rest of the string is regarded as garbage. Now, there are two ways of dealing with this problem. The first one is to insert a backslash in front of the apostrophe. And that clears the syntax error.

So, let‘s just try that out by displaying the value and save the page and view it in a browser. So, the value of book is being displayed in the browser. Another way of dealing with this is to remove that backslash there and change the opening and closing quotes to double quotes.

In this case, it‘s so much more elegant solution. But what if I want to have double quotes around the title of the book. Well, one way in which I could do that is to have single quotes surrounding the double quotes. But then, that leaves the problem in the middle here with the apostrophe. So, I again need to use a backslash to solve that. And if we save it and refresh the browser, you can see that we now have the double quotes surrounding the title and the apostrophe in the middle. And you‘ve probably guessed by now that if you want to use double quotes inside a double quoted string, then you need to use a backslash in front of the quote. You need to get rid of that one there.

And so, we now have a double quoted string with an apostrophe in the middle and it also has double quotes which are escaped by preceding them with a backslash. But having more backslashes than you really need to have in a string looks rather ugly, and it also makes it difficult to read. So, I think that the best way of handling this particular situation is to have single quotes, to escape the apostrophe and a single quote at the end.

But what if I want to incorporate this variable into a string? Well, let‘s try that out. I‘ll start off with single quotes, I love book, and save that. We have no syntax errors. But if I refresh the browser, you‘ll see that it says I love dollar book. The reason for this is that anything enclosed in single quotes is treated as literal text. So, the variable, you don‘t get the value of the variable, you get the variable itself.

But if you change that to double quotes, let‘s see what happens. Refresh the browser. We now get the value of the variable, reflected in the string. So, if you ever want to incorporate a variable in a string, you need to use double quotes. The double quotes act as a signal to the PHP engine to process any variables which are inside the string. Double quotes process not only variables, but also what are known as escape characters.

This is a full list of the escape characters that are used in PHP double quoted strings. The ones you‘re most likely to encounter are the first three, double quote, new line, and carriage return. You use new line and carriage return when building scripts for email. Because double quotes are useful in processing variables and escape characters, many people use them all the time. But technically speaking, using double quotes when you don‘t need them to process any variables or escape characters, is inefficient.

My preference is to use single quotes, unless the string contains either variables or escape characters.

Storing text as strings