Summary of string definition tips in PHP

Source: Internet
Author: User
Tags echo command first string

There are multiple methods to define the value of a string in PHP. There are a lot of tips, which are specially collected and sorted here for backup.

1. The most common format is a character referenced by double quotation marks or single quotation marks, such

$ STR = "I Am a string enclosed by double quotation marks"; or $ STR = 'I am a string enclosed by single quotation marks ';

The difference between the two is that strings enclosed by double quotation marks can directly insert variables without a connector, while single quotation marks do not process the variable format in the string, so if you want to define the string of the concatenated variable, it will be faster to use single quotes.

It is worth noting that, after a manual test, if a pure string is defined and no variables are concatenated, double quotation marks are a little more efficient.

2. variables can be directly inserted into a string enclosed in double quotes. For example, the variable name can be written directly at the location where the variable is to be inserted or the variable name can be wrapped in braces,

$ Str1 = "the content of the first string variable is: $ STR, that's all. "; $ STR will be replaced with the value of the corresponding variable during output.

Supplement:If the variable name is embedded in the form of "text {$ STR} text", the variable name is what you expected to specify, and if the variable name is embedded in the form of "text $ STR text, at this time, the variable will be processed as "$ STR text" instead of the expected "$ Str ". This is because when variables are not explicitly specified using braces, PHP uses word segmentation characters as the boundary for automatic variable processing. Word Segmentation characters are generally space punctuation marks.

3. If you do not want to directly insert a variable to define a string, you can use a dot to connect multiple strings, such

$ Str1 = 'the content of the first string variable is '. $ Str.', that's all. ';

4. What should I do if I encounter a string that requires writing multiple lines of books? For example, we need to define a JS script. Of course, it is okay if I write the script in one line, but if the script is longer, in addition, there will be many escape characters. If the script is finally formed, it may become a pot of porridge. Not only is the format not easy to read, but it will also be cool to modify it if something goes wrong. Is there a way to define a string as multiple rows and output the escape characters such as quotation marks directly? The answer is yes. php has long anticipated this demand. We can define a multi-line string as follows:

$ STR = <Str
'OK', "hello"
I will
{$ Str1}
STR;

Among them, three less than signs represent the input of a multi-line string. STR is a string separator. You can define the name of the delimiter by yourself. The content between the two delimiters is a multi-line string. The single quotation marks and double quotation marks can be directly output without escaping. Of course, variables can also be inserted directly. The output will automatically replace the variable value.

5. Add a trick. When Echo is used for string output, if the string is concatenated, you can use a comma to connect the strings and variables to be spliced, such

Echo 'first', "second", $ STR, "end ";

It is said that the output speed is faster because ECHO can accept multiple parameters and output them in order. In fact, commas are not concatenated strings, instead, different strings and variables are transmitted to the echo command as parameters.

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.