PHP: Detailed explanation of string data type examples

怪我咯
Release: 2023-03-07 15:34:01
Original
5356 people have browsed it

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=&#39;PHP中文网&#39;;
?>
Copy after login

Double quotes

<?php

$a="PHP中文网";
?>
Copy after login

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=&#39;PHP中文网&#39;;                                              //声明一个字符串变量
echo "$i";                                                   //用双引号输出
echo "<p>";                                                  //输出短标记
echo &#39;$i&#39;;                                                   //用单引号输出
?>
Copy after login

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将花费一些时间来处理字符串的转移和变量的解析,因此,在定义字符串时,如果没有特别的要求,我们最好使用单引号

界定符

界定符(<<<)是从PHP4.0开始支持的。在使用后接一个标识符,然后是字符串,最后是同样的标识符结束字符串。界定符的格式如下:

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

其中,str为指定的标识符

界定符示例

代码如下:

<?php
header("Content-type:text/html;charset=utf-8");              //设置编码
$i=&#39;PHP中文网&#39;;                                              //声明一个字符串变量
echo <<<std
这和双引号没什么区别,\$i同样可以被输出出来。<p>
\$i的内容为:$i
std
?>
Copy after login

代码运行结果:

PHP: Detailed explanation of string data type examples

实例详解:

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

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

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

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
Escape charactersInput Output
\nNewline
\rEnter
\tHorizontal tab
\\ Backslash
Regular expression matches a character represented by octal notation
Regular expression The formula matches a character represented by hexadecimal notation