Home  >  Article  >  Backend Development  >  PHP: Detailed explanation of string data type examples

PHP: Detailed explanation of string data type examples

怪我咯
怪我咯Original
2017-04-27 17:03:195232browse

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

Double quotes

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

";                                                  //输出短标记
echo '$i';                                                   //用单引号输出
?>

The example running result is as follows:

PHP: Detailed explanation of string data type examples

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

##\$Dollar sign\'Single quotes\"Double quotes##\[0-7]{1,3}\x[0-9A-Fa-f]{1,2}

PHP: Detailed explanation of string data type examples这里有一点要注意的是,\n和\r在Windows系统中没有什么区别,都可以当作回车符。但是在Linux系统中则是两种效果,在Linux中,\n表示换到下一行,却不会回到行首;而\r表示光标回到行首,但仍然在本行,如果有使用Linux操作系统的,可以尝试一下。

如果我们对非转义字符使用了“\”,那么在输出时,“\”也会跟着一起被输出。

在定义一些简单的字符串是,使用单引号是一个更好的处理方式。如果使用双引号,PHP将花费一些时间来处理字符串的转移和变量的解析,因此,在定义字符串时,如果没有特别的要求,我们最好使用单引号

界定符

界定符(

$string=<<< str
 要输出的字符串
str

其中,str为指定的标识符

界定符示例

代码如下:


\$i的内容为:$i
std
?>

代码运行结果:

PHP: Detailed explanation of string data type examples

实例详解:

在上面的示例中,我们使用了界定符输出变量的值,可以看到它和双引号没什么区别,包含的变量也被替换成实际数值。

PHP: Detailed explanation of string data type examples结束标识符必须单独另起一行,并且不允许有空格。在标识符前后有其他字符或符号,也会发生系统错误,包括注释。

下一节,我们给大家介绍标量数据类型中的“整型(integer)”。

Escape characters Input Output
\n Newline
\r Enter
\t Horizontal tab
\\ Backslash
Regular expression matches a character represented by octal notation
Regular expression The formula matches a character represented by hexadecimal notation

The above is the detailed content of PHP: Detailed explanation of string data type examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn