Difference between double quotation marks and single quotation marks in a php string

Source: Internet
Author: User

[Php] <? Php/* the string has already been mentioned when talking about variables. How can this problem be solved? A: Previously, I only introduced the variable types such as string, but I did not go into details about its definition, nature, function, and so on. 2nd: string, which is a very important concept in PHP. in this case, you want to perform some operations on strings on the development website. basically, the processing function of the string you think of already has related functions in the system. since its birth, PHP was developed for the WEB, because string processing is a highlight of PHP during website development. 3rd: strings are most frequently used during development and interviews. in PHP, 1 is a string and 2 is an array function, which is very useful. * // * What is a string? A: A string is a string of mutton. * // * 1: How to declare a string variable answer: Double quotation marks (single quotation mark) */$ str1 = "hello"; echo $ str1, '<br/> '; // $ str2 = "hello," jack "; // This string is a news title, and double quotation marks are easily displayed in the title. /** think: When double quotation marks are used to declare strings, double quotation marks cannot appear internally, because double quotation marks are treated as the "boundary" of strings. Therefore, double quotation marks appear internally, it causes ambiguity in interpretation. in this case, you can use escape characters, that is, \ "escape to indicate" Are there any other escape characters? Thought: \ ", used as" understanding. What should I do if I really want to represent the backslash ?, A: \ indicates the backslash. \ is there any escape character? A: \ n, \ r, \ $, etc. Convert \ n to linefeed \ r to carriage return. **/$ str2 = "hello, \" jack \", <br/> "; echo $ str2; $ str3 =" hello \ "; echo $ str3, '<br/> '; $ str4 = "hello \ n \ r \ n \ rworld"; echo $ str4, '<br/>'; $ str5 = "hello $ str3 "; echo $ str5, '<br/>'; // hello \, resolve $ str3 as a variable $ str6 = "world \ $ str3 "; // here \ $ is converted into a common string $, instead of a variable flag. echo $ str6;?> <? Php // look at the single quotes $ str1 = 'hello'; echo $ str1, '<br/>'; // $ str2 = 'hello' jack ''; // a string is the news title, and the title is enclosed in single quotes. // What should I do if the single quotes appear in single quotes? // 'Is used to define the boundary of the string. // Therefore, if you want to see' in the string, you have to use an escape character to indicate $ str2 = 'Hello \ 'Jack \''; echo $ str2, '<br/>'; // \ is used to escape single quotes. What should I do if I really want to represent. // \ to indicate \ $ str3 = 'Hello \ '; echo $ str3;?> <? Php // common interview questions // 1: What is the difference between a single double quotation mark defining a string? $ Age = 29; $ str1 = 'Hello \ n \ r \ t $ age'; $ str2 = "hello \ n \ r \ t $ age"; echo $ str1, '<br/>', $ str2;/* difference in escape: single quotation marks. The system does not perform complex escape. Only escape \ ', \, and others are output as is. double quotation marks, escape more, \ ", \, \ r, \ n, \ t, \ $, etc. differences in variable interpretation: single quotes do not parse the variable name of the $ string, while double quotes will try to parse $ as a variable name. * // 2: A single double quotation mark defines a string. Which one is faster? // A: single quotes can be parsed more quickly, because too much conversion and variable parsing are not required. // when can single or double quotes be used? /* Www.2cto.com: large text, such as news titles, text content, and self-introduction. Use 'to resolve the text quickly. however, sometimes, variables must be included in a string, such as $ id = 5 when splicing SQL statements; $ SQL = "select * from user where id = $ id "; // if a single reference is used at this time, the statement becomes .. id = $ id. // at this time, it is appropriate to use double quotes. parse $ id. The statement is parsed .. id = 5; */?>

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.