Storing text as strings

Source: Internet
Author: User

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 cocould 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 uugly, and it also makes it difficult to read. so, I think that the best way of handling this particle situation is to have single quotes, to escape the apostrophe and a single quote at the end.

But what if I want to induplicate ate 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 in1_ate 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, please 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.