PHP data type string

Strings are all our visible and invisible characters, which are what we say in daily life, which is what I want to say: "Xiao Ming is so handsome" or "Sister Feng, I love you!". Strings are all the characters that people can see that I want to express.

For example, you can see the characters:

I would like to become you, the prince you love, with a house, a car and money. ^@@@#@ my name is xiaoshenyang

The text that can be seen above, HTML tags, special characters and English, etc., we all think of them as strings.

Note: Invisible characters will not be explained for the time being, because they cannot be represented in the text. Just pay attention to them gradually in the following experiments. For a clearer representation, you can also watch the video on strings produced by the PHP Academy that accompanies this book.

There are three ways to declare a string in PHP language:

1. Use single quotes to declare

2. Use double quotes to declare

3. Use Character delimiter declaration (used when a very large string needs to be entered)

1. Single quotation mark declaration

use English half-character Single quotes, wrap the string

2. Double quotes declare the string
Add double quotes on both sides of the string.

3. Character declaration
1). Write three less than signs (<<<) after the equal sign after the variable .
2). Then write characters after <<< (English uppercase characters are recommended). As in the following example: ABC
3). Then change the line and write any characters you want
4). After writing, go to the top line. At the beginning of the line, write the characters following <<< and a semicolon. As in the following example: ABC;

 加上一段 距离的话 我想说:

思想有多远,你就跟我滚多远

ABC; ?>

What is the difference between double quotes and single quotes?

[Important knowledge points] Among the PHP interview questions, high-probability interview questions (it is recommended to memorize and experiment more than three times)

1. Double quotes parse variables, but single quotes do not parse variables.

2. Insert the variable inside the double quotes. If there are English or Chinese characters behind the variable, it will splice the character and the variable together and treat it as a whole variable. Be sure to separate special characters, such as spaces, after the variable.

3. If you insert a variable within double quotes and do not want spaces behind it, you can wrap the variable in braces.

4. Double quotes parse escape characters, single quotes do not parse escape characters. However, single quotes can parse \' and \

5. Single quotes are more efficient than double quotes, use single quotes as much as possible

6. Double numbers and single quotes can be inserted into each other! ! ! Insert single quotes between double quotes, insert variables between single quotes, and the variable will be parsed.

7. The magical string splicing glue - (.) dot, used to splice strings.

8. We treat the delimiter declaration string as the same function as double quotes.

Experimental example

1. Double quotes parse variables, but single quotes do not parse variables

Open in the browser, the running results are as follows:

QQ截图20161114092822.png

Through the above example, we found. The variable between the double quotes is enough for execution and display. Let's try changing the double quotes on both sides of the $str string to single quotes and see the execution result again:

QQ截图20161114093040.png

It is found that $shouji is displayed directly instead of the display result of double quotes.

Through the above example, we proved that: double quotes execute variables, while single quotes do not execute variables. We get an important conclusion:

Double quotes need to parse variables, so double quotes are not as efficient as single quotes. For higher efficiency, we usually use single quotes.

2. Insert the variable inside the double quotes. If there are English or Chinese characters behind the variable, it will concatenate the character and the variable and treat it as a whole variable. It can be separated by special characters such as spaces.

However, in the string declared by $str, we add $huaqiangu followed by a space and then aaaa. The code is as follows:

Everyone found that the result displayed during execution is not the same. There is no longer a blank page, but there is content in this page. As follows:

QQ截图20161114093257.png

Let’s change the code again:

Let’s open the page once and execute the code once to see if the result changes. :

php中文网!aaaa

Therefore, our second point is proved.

3. If you insert a variable within double quotes and do not want spaces after it, you can wrap the variable in braces.

Then, what if I don’t want to have spaces or special symbols at the end and want to directly display the variable $huaqiangu? - Our solution is to wrap the variable in curly braces. The code is as follows:

So, you will find that there are no special symbols or spaces behind Hua Qiangu, which achieves the display result we want. The demonstration effect is as follows:

QQ截图20161114093552.png

4. Double quotes parse escape characters, single quotes do not parse escape characters. However, single quotes can parse \' and \

. The fourth string is difficult to observe. There are some escape characters, but the most commonly used ones are:
\n Enter
\t Indentation

Let’s first understand what \n and \t are through experiments.

There is one thing to note when doing this experiment. The effect cannot be seen by displaying the web page, as follows:

QQ截图20161114093510.png

Why can’t I see the effect? What do \n and \t mean? I can't tell that double quotes parse \n and \t? ——Because you did not right-click to view the source code. Let's click it and see the effect.

Step 1: Display the web page source code

Step 2: View the results displayed by the HTML source code:

Let’s compare the string variables :

$string = "Come to the PHP Chinese website every day\nGive your dream\ta chance";

\nBehind the PHP Academy, in the source code The displayed result is the same as pressing the Enter key.
\tBetween giving a dream and a chance. The displayed result is the effect of several spaces. \tIt is equivalent to the effect of pressing a tab key on the computer.

However, we also execute the above code, but change the $string double quotes to single quotes:

Executeand view it onetime Look at the effect:

QQ截图20161114093821.png

You will find that the \t and \n between the single quotes are displayed directly, instead of pressing the Enter key and tab once. The effect after the key.
Thus:

Double quotes perform escaping characters while single quotes do not.

5. Escape character \ (use of backslash)

If we declare a variable $beizi , what if we want to add a double quote between the double quotes declared in $beizi to display the output?

If the code is written as above, the code will report an error. The error result is as follows:

Because the string: "More than the vast majority of people are born with disabilities, but "We are constantly making life a comedy" must be written between double quotes. And between double quotes There is also a double quotation mark. This means that the string statement ends at "but", and "we are constantly making life a comedy". This is really

. What to do?

Answer: We need to remove the original meaning of the double quotes and add a \ (backslash, the professional term for computers). That’s it.

You can execute the above code again and find that the error is no longer reported. By the same token, when you want to insert a single quote into a single quote and display it, you can also add (backslash, escape character) in front of the single quote in the string declared by the single quote to change the meaning of the single quote ( limited character range) removed.

6. Double quotes and single quotes can be inserted into each other! ! ! Insert single quotes between double quotes, insert variables between single quotes, and the variable will be parsed.

The code is as follows:

The execution result is as follows:

QQ截图20161114093923.png

## 7. The magical string splicing glue - (.) dot, used to splice strings.

The above code is, we once said that double quotes can parse variables, there is a problem:

The efficiency is not as high as pure single quotes

So question 1: What should I do if I want to change to the most efficient way?

Question 2: I have multiple strings, what should I do if I want to splice them together?

Then, we need to use the magic glue: (.) dots to splice strings.

'; $buran = '不然连实习的机会都没有
'; $mimang = '把别人用来迷茫的时间拿到PHP中文网
'; $xuexi = '学习PHP
'; //我们可以把字符串全部拼接起来。 echo $shixi . $buran . $mimang . $xuexi; ?> 因此,刚刚的问题一的代码我们可以改为:

8. We treat the delimiter declaration string as the same function as double quotes.

 加上\n一段 距离的话 我想说:

思想有多远,你就跟我滚多远

ABC; echo $dingjie; ?>

You can execute it and find that $weilai, $mimang, \t\n can all be executed, and double quotes and single quotes can be displayed. This is the characteristic of delimiters.

#Declare it again: Every item in the string declaration must be remembered, it is very common. And the probability of interview questions appearing is very high!


Invisible characters: mainly some special symbols specified by the computer. For example: carriage return (\n), indent (\t), etc.


Double quotes execute variables, so


Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!