What are the string delimiters in php?
php字符串定界符有两种:1、heredoc定界符,在“
本教程操作环境:windows7系统、PHP8.1版、DELL G3电脑
PHP定界符
PHP定界符是从PHP4.0版本开始支持的。
定界符用于定义一段格式化的文本,格式化是指文本中的格式将被保留,所以文本中不需要使用定义符。在使用时后接一个标识符,然后是格式化的文本(即字符串),最后使用同样的标识符结束字符串,这段格式化的文本也可以称为长字符串。
为什么使用定界符
因为PHP是一个Web编程语言,在编程过程中难免会遇到使用echo来输出大段的HTML代码或者JavaScript脚本的情况。如果用传统字符串输出的话,肯定要使用大量的转义字符来对字符串中的特殊字符进行转义,比如单引号‘’、双引号“”等等,难免会出现语法错误。
而PHP中定界符能够定义一段较长的字符串,并且可以按照原样输出在其内部的东西,包括换行、缩进等格式,在定界符中任何特殊字符都不需要转义,而且定界符中的变量也能被解析。这也是为什么 PHP 要引入定界符的原因之一。
php中字符串定界符有几种
PHP中有两种定界符:heredoc(双引号定界符)和nowdoc(单引号定界符)
1、“Heredoc”定义方式
heredoc 句法结构:。在该运算符之后要提供一个标识符,然后换行。接下来是字符串 string 本身,最后要用前面定义的标识符作为结束标志。
结束标识符可以使用空格或制表符(tab)缩进,此时文档字符串会删除所有缩进。 在 PHP 7.3.0 之前的版本中,结束时所引用的标识符必须在该行的第一列。
而且,标识符的命名也要像其它标签一样遵守 PHP 的规则:只能包含字母、数字和下划线,并且必须以字母和下划线作为开头。
<?php $str = <<<EOF url: https://www.php.cn/ EOF; echo $str; ?>
PHP 7.3.0 之后的基础 Heredoc 示例
<?php // 无缩进 echo <<<END a b c \n END; // 4 空格缩进 echo <<<END a b c END;
如果结束标识符的缩进超过内容的任何一行的缩进,则将抛出 ParseError 异常:
示例:结束标识符的缩进不能超过正文的任何一行
<?php echo <<<END a b c END;
以上例程在 PHP 7.3 中的输出:
PHP Parse error: Invalid body indentation level (expecting an indentation level of at least 3) in example.php on line 4
制表符也可以缩进结束标识符,但是,关于缩进结束标识符和内容, 制表符和空格不能混合使用。在以上任何情况下, 将会抛出 ParseError 异常。 之所以包含这些空白限制,是因为混合制表符和空格来缩进不利于易读性。
示例:内容(空白)和结束标识符的不同缩进
<?php // 以下所有代码都不起作用。 // 正文(空格)和结束标记(制表符),不同的缩进 { echo <<<END a END; } // 在正文中混合空格和制表符 { echo <<<END a END; } // 在结束标记中混合空格和制表符 { echo <<<END a END; }
以上例程在 PHP 7.3 中的输出:
PHP Parse error: Invalid indentation - tabs and spaces cannot be mixed in example.php line 8
内容字符串的结束标识符后面不需要跟分号或者换行符。 例如,从 PHP 7.3.0 开始允许以下代码:
示例:在结束标识符后继续表达式
<?php $values = [<<<END a b c END, 'd e f']; var_dump($values);
以上例程在 PHP 7.3 中的输出:
array(2) { [0] => string(11) "a b c" [1] => string(5) "d e f" }
2、“Nowdoc”定义方式
就象 heredoc 结构类似于双引号字符串,Nowdoc 结构是类似于单引号字符串的。Nowdoc 结构很象 heredoc 结构,但是 nowdoc 中不进行解析操作。这种结构很适合用于嵌入 PHP 代码或其它大段文本而无需对其中的特殊字符进行转义,与 SGML 的<![CDATA[ ]]>
结构是用来声明大段的不用解析的文本类似,nowdoc 结构也有相同的特征。
一个 nowdoc 结构也用和 heredocs 结构一样的标记 <<<
, 但是跟在后面的标识符要用单引号括起来,即 <<<'EOT'
。Heredoc 结构的所有规则也同样适用于 nowdoc 结构,尤其是结束标识符的规则。
示例:
<?php echo <<<'EOD' Example of string spanning multiple lines using nowdoc syntax. Backslashes are always treated literally, e.g. \\ and \'. EOD;
<?php /* 含有变量的更复杂的示例 */ class foo { public $foo; public $bar; function __construct() { $this->foo = 'Foo'; $this->bar = array('Bar1', 'Bar2', 'Bar3'); } } $foo = new foo(); $name = 'MyName'; echo <<<'EOT' My name is "$name". I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should not print a capital 'A': \x41 EOT; ?>
注意:
Nowdoc 结构是在 PHP 5.3.0 中加入的。
推荐学习:《PHP视频教程》
The above is the detailed content of What are the string delimiters in php?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
