PHP data type s...LOGIN

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. <html><title></title></html> ^@@@#@ 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

<?php
//声明字符串变量$zhifu

$zhifu = '曾经有操蛋的爱情摆在我面前,我珍惜了。当我得到的时候才感到后悔莫及。如果非要在这段爱情前面加上一段三个字,我愿意说三个字:滚犊子';

//你可以放XAMPP指定的目录下,新建一个文件叫str.php。然后访问一下http://127.0.0.1/str.php试试。会不会显示这句话。

echo $zhifu;

?>

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

<?php
//声明字符串变量$str
$str = "如果非要在滚犊子前面加上一个时间的话我愿意是马上。";

echo $str;
?>

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;

<?php

$dingjie = <<<ABC
  如果
       非要在这个滚犊子
   前
       面<br />
      加上一段
   <i>距离的话</i>
   我想说:<h1>思想有多远,你就跟我滚多远</h1>
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

<?php
//声明变量$shouji
$shouji = '为了苹果手机去卖肾';
//在双引号中放$shouji 然后echo 一下是什么效果呢?
$str = "$shouji 会不会显示呢?";
//输入$str试试
echo $str;
?>

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.

<?php
$php = 'php中文网';

$str = "$phpaaaa";
//你会发现输出$str,什么都没有在页面中显示
echo $str;
?>

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

<?php
$php = 'php中文网';
//中间加了空格哟
$str = "$php aaaa";

echo $str;
?>

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:

<?php
$php = 'php中文网';
//中间加了空格哟
$str = "$php!aaaa";

echo $str;
?>

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:

<?php
$php = 'php中文网';
//中间加了空格哟
$str = "{$php}aaaa";

echo $str;
?>

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.

<?php
//声明一个字符串,记住是双引号
$string = "每天来PHP中文网\n给梦想\t一个机会";
echo $string;
?>

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:

<?php
//声明一个字符串,记住是双引号
$string = '每天来PHP中文网\n给梦想\t一个机会';
echo $string;
?>

Execute and view it one time 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?

<?php

//要在$beizi的字符串中显示一个双引号怎么办?
$beizi = "多于绝大多数的人出生就是杯具,但是"我们在不断的让人生变为喜剧";

echo $beizi;
?>

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.

<?php

//要在$beizi的字符串中显示一个双引号怎么办?
$beizi = "多于绝大多数的人出生就是杯具,但是\"我们在不断的让人生变为喜剧";

echo $beizi;
?>

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.

<?php

//要在$beizi的字符串中显示一个双引号怎么办?
$shengyang = 'i\'m xiaoshengyang';

echo  $shengyang;
?>

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:

<?php

$legend = '猛虎';

$NoAlike = "心有'$legend',细嗅蔷薇";

echo $NoAlike;

?>

The execution result is as follows:

QQ截图20161114093923.png

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

<?php
$php = 'PHP中文网';
//中间加了空格哟
$str = "{$php}aaaa";

echo $str;
?>

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.

<?php

$shixi = '大学4年要好好学习<br />';

$buran = '不然连实习的机会都没有<br />';

$mimang = '把别人用来迷茫的时间拿到PHP中文网<br />';

$xuexi = '学习PHP<br />';

//我们可以把字符串全部拼接起来。
echo $shixi . $buran . $mimang . $xuexi;

?>
因此,刚刚的问题一的代码我们可以改为:
<?php
$php = 'PHP中文网';
//中间加了空格哟
$str = $php . 'aaaa';

echo $str;
?>

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

<?php

$weilai = '努力才有未来';
$mimang = '迷茫的原因是没有目标';

$dingjie = <<<ABC
  如果
           $weilai
       非要\t在这"个滚"犊子
   前
           '$mimang'
       面<br />
      加上\n一段
   <i>距离的话</i>
   我想说:<h1>思想有多远,你就跟我滚多远</h1>
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


Next Section

<?php //声明字符串变量$zhifu $zhifu = '曾经有操蛋的爱情摆在我面前,我珍惜了。当我得到的时候才感到后悔莫及。如果非要在这段爱情前面加上一段三个字,我愿意说三个字:滚犊子'; //你可以放XAMPP指定的目录下,新建一个文件叫str.php。然后访问一下http://127.0.0.1/str.php试试。会不会显示这句话。 echo $zhifu; ?>
submitReset Code
ChapterCourseware