What is a string?
A string is a continuous sequence of characters, consisting of numbers, letters and symbols. Each character in the string takes up only one byte.
In PHP, there are three ways to define strings, namely single quotes ('), double quotes (") and delimiters (
Single quotes and double quotes are often used to define strings. They are defined as follows:
Single quotes
<?php $a='PHP中文网'; ?>
Double quotes
<?php $a="PHP中文网"; ?>
The difference between single quotes and double quotes is that the variables contained in double quotes will be automatically replaced with actual values, while the variables contained in single quotes will be output as ordinary characters. What should I change in this paragraph? What does it mean? Let’s take a look at an example
An example of the difference between single quotes and double quotes defining strings
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $i='PHP中文网'; //声明一个字符串变量 echo "$i"; //用双引号输出 echo "<p>"; //输出短标记 echo '$i'; //用单引号输出 ?>
The example running result is as follows:
Detailed explanation of the example:
In this example, we use single quotes and double quotes to output the same variable, but the output results are completely different , as can be seen from the above figure, the result we use double quotes to output is the value of the variable, while the result we use single quotes to output is the string "$i"
In addition to the above differences, again. There is a difference in the use of transfer characters between the two. When using single quotes, you only need to transfer the single quotes " ' ", but when using double quotes ("), you must also pay attention to " " ", " $ " and other characters. For use, these special characters must be displayed through the escape character "\". Commonly used escape characters are as follows:
Commonly used escape characters
Escape characters | Input Output |
\n | Newline |
\r | Enter |
\t | Horizontal tab |
\\ | Backslash |
Dollar sign | |
Single quotes | |
Double quotes | |
Regular expression matches a character represented by octal notation | |
Regular expression The formula matches a character represented by hexadecimal notation |