Table of Contents
单引号
双引号
heredoc语法结构
nowdoc语法结构
Home Backend Development PHP Problem What are the representation methods of php strings?

What are the representation methods of php strings?

Jul 08, 2021 pm 04:05 PM
php string

表示方法有:1、用单引号把字符包围起来,语法“'字符'”;2、用双引号把字符包围起来,语法“"字符"”;3、 heredoc语法结构,语法“<<

What are the representation methods of php strings?

本教程操作环境:windows7系统、PHP8.1版,DELL G3电脑

在PHP中有四种方式可以表示字符串,分别是单引号、双引号、heredoc语法结构、nowdoc语法结构。

单引号

定义字符串的最简单的方法,用单引号把字符包围起来。使用单引号时,如果要表达单引号或者反斜杠需要使用反斜杠“\”进行转义。单引号中只对这两个字符进行转义。在单引号中的变量和特殊字符的转义序列不会被替换。单引号中的字符可以是多行的。

<?php
$name = &#39;php&#39;;
@header("Content-type: text/html; charset=utf-8"); 
echo &#39;最简单的字符串表达形式&#39;."\n";
 
echo &#39;可以是
多行的&#39;."\n";
 
echo &#39;单引号"\&#39;"和反斜杠"\\"需要使用"\"转义&#39;."\n";
 
echo &#39;"\\"与"\"是一样的&#39;."\n";
 
echo &#39;其他特殊字符如\n、\r和变量$name是不会转换的&#39;."\n";
?>
Copy after login

双引号

如果字符被双引号包围,PHP将对其中的一些特殊字符进行解析,双引号可解析的特殊字符如下,双引号字符串最重要的特性是变量解析。

\n:换行(ASCII 字符集中的 LF 或 0x0A (10))
\r;回车(ASCII 字符集中的 CR 或 0x0D (13))
\t;水平制表符(ASCII 字符集中的 HT 或 0x09 (9))
\v;垂直制表符(ASCII 字符集中的 VT 或 0x0B (11))(自 PHP 5.2.5 起)
\e;Escape(ASCII 字符集中的 ESC 或 0x1B (27))(自 PHP 5.4.0 起)
\f;换页(ASCII 字符集中的 FF 或 0x0C (12))(自 PHP 5.2.5 起)
\\;反斜线
\$;美元标记
\";双引号
\[0-7]{1,3};符合该正则表达式序列的是一个以八进制方式来表达的字符
\x[0-9A-Fa-f]{1,2};符合该正则表达式序列的是一个以十六进制方式来表达的字符

<?php
$name = &#39;php&#39;;
@header("Content-type: text/html; charset=utf-8"); 
 
echo "双引号中单引号&#39;不需要转义反斜杠\也不需要转义双引号\"需要转义\n";
 
echo "双引号也可以是
		多行的\n";
 
echo "\"\\\\\"与\"\\\"是一样的,但是如果想要表示两个反斜杠则需要使用四个反斜杠\n";
 
echo "还可以转义其他一些特殊字符,如:\\\$转义为\$,\\x41转义为\x41\n";
 
echo "还可以解析变量,如:\\\$name解析为$name\n";
?>
Copy after login

heredoc语法结构

heredoc语法结构是使用<<<加开始标记和结束标记。在<<<之后提供一个标识符(此标识符的命名需要遵循PHP标签的命名规则,即只能包含字母、数字以及下划线,并且不能以数字开头),然后换行,接下来是字符串本身, 最后用前面定义的标识符做为结束标记。

需要注意的是,开始标记后必须换行,结束标记所在行除了分号“;”外,不能有包含其他字符包括缩进,分号前后也不能有任何空白或制表符等。

在heredoc结构中,单引号、双引号与回车符(\r)与单个反斜杠是不需要转义的,如果要表示多个反斜杠则需要进行转义,其他转义字符与双引号支持的转义字符相同。heredoc结构类似于双引号字符串。

heredoc结构可以用于变量初始化,也可以用在函数参数中传递数据。从PHP5.3起,如果heredoc中不包含变量,那么也可以用来初始化静态变量和类中的属性、常量。还可以通过双引号声明开始标识符。

<?php
@header("Content-type: text/html; charset=utf-8"); 
 
$name = &#39;php&#39;;
function foo(){
	static $str = <<<EOS
\n
\r
\t
\v
\e
\f
\
\\
\\\\\
\$name
\"
\&#39;
\x41
\072
EOS;
echo $str;
}
foo();
 
/* 含有变量的更复杂示例 */
class foo
{
    
	const cons= <<<"EOD"
cons
EOD;
	var $test= <<<EOD
"test"
EOD;
	var $bar;
    function foo()
    {
        $this->bar = array(&#39;Bar1&#39;, &#39;Bar2&#39;, &#39;Bar3&#39;);
    }
}
 
$foo = new foo();
echo foo::cons."\n";
echo <<<EOT
heredoc中支持更复杂的格式如:
\$foo->test=> $foo->test.
{\$foo->bar[1]}=>{$foo->bar[1]}.
EOT;
?>
Copy after login

nowdoc语法结构

nowdoc语法结构是PHP5.3引入的,与heredoc语法结构类似,使用<<<加开始标记和结束标记。但是在<<<后面的开始标识符必须用单引号包围起来,heredoc的所有规则同样适用于nowdoc结构,尤其是结束标识符的规则。

nowdoc结构类似于单引号字符串。nowdoc中不进行解析操作,适用于嵌入PHP代码或大段文本而无需对其中的特殊字符进行转义。与SGML中的结构声明大段的不用解析的文本类似。

<?php
@header("Content-type: text/html; charset=utf-8"); 
 
$name = &#39;php&#39;;
function foo(){
	static $str = <<<&#39;EOS&#39;
$name
\n
\r
\t
\v
\e
\f
\
\\
\\\\\
\$name
\"
\&#39;
\x41
\072
EOS;
echo $str;
}
foo();
 
/* 含有变量的更复杂示例 */
class foo
{
    
	const cons= <<<&#39;EOD&#39;
cons
EOD;
	var $test= <<<&#39;EOD&#39;
"test"
EOD;
	var $bar;
    function foo()
    {
        $this->bar = array(&#39;Bar1&#39;, &#39;Bar2&#39;, &#39;Bar3&#39;);
    }
}
 
$foo = new foo();
echo foo::cons."\n";
echo <<<&#39;EOT&#39;
nowdoc中不支持复杂的格式如:
\$foo->test=> $foo->test.
{\$foo->bar[1]}=>{$foo->bar[1]}.
EOT;
?>
Copy after login

推荐学习:《PHP视频教程

The above is the detailed content of What are the representation methods of php strings?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles