Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial php中<<<是什么符号

php中<<<是什么符号

Jun 23, 2016 pm 02:01 PM

php中


回复讨论(解决方案)

定界符    echo  My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?> 

另一种给字符串定界的方法使用定界符语法(“

定界符
另一种给字符串定界的方法使用定界符语法(“
结束标识符必须从行的第一列开始。同样,标识符也必须遵循 PHP 中其它任何标签的命名规则:只能包含字母数字下划线,而且必须以下划线或非数字字符开始。 

heredoc句法结构:
结束时所引用的标识符必须在一行的开始位置, 而且,标识符的命名也要像其它标签一样遵守PHP的规则:只能包含字母、数字和下划线,并且不能用数字和下划线作为开头。 
要注意的是结束标识符这行除了 可能有一个分号(;)外,绝对不能包括其它字符。这意味着标识符不能缩进,分号的前后也不能有任何空白或tabs。更重要的是结束标识符的前面必须是个被本地操作系统认可的新行标签,比如在UNIX和Mac OS X系统中是\n ,而结束标识符(可能有个分号)的后面也必须跟个新行标签。 

如果不遵守该规则导致结束标签不“干净”,PHP将认为它不是结束标识符而继续寻找。如果在文件结束前也没有找到一个正确的结束标识符,PHP将会在最后一行产生一个句法错误。 

Heredocs结构不能用来初始化class,而从PHP 5.3以后,则该限制只能用在包含变量的情况下。 

Example #1 非法的示例

class foo {
    public $bar =  bar
EOT;
}
?> 
Heredoc结构就象是没有使用双引号的双引号字符串, 这就是说在heredoc结构中引号不用被替换,但是上文中列出的字符 (\n等)也可使用。 变量将被替换,但在heredoc结构中字符串表达复杂变量时,要格外小心。 

Example #2 Heredoc结构的字符串示例

$str =  Example of string
spanning multiple lines
using heredoc syntax.
EOD;

/* 含有变量的更复杂示例 */
class foo
{
    var $foo;
    var $bar;

    function foo()
    {
        $this->foo = 'Foo';
        $this->bar = array('Bar1', 'Bar2', 'Bar3');
    }
}

$foo = new foo();
$name = 'MyName';

echo  My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?> 
以上例程会输出:

My name is "MyName". I am printing some Foo.
Now, I am printing some Bar2.
This should print a capital 'A': A也可以把Heredoc结构用在函数参数中来传输数据: 

Example #3 Heredoc结构在参数中的示例

var_dump(array( foobar!
EOD
));
?> 
在PHP 5.3.0以后,也可以用Heredoc结构来初始化静态变量和类的属性和常量: 

Example #4 使用Heredoc结构来初始化静态值

// 静态变量
function foo()
{
    static $bar =  Nothing in here...
LABEL;
}

// 类的常量、属性
class foo
{
    const BAR =  Constant example
FOOBAR;

    public $baz =  Property example
FOOBAR;
}
?> 
在PHP 5.3.0中还在Heredoc结构中用双引号来声明标志符: 

Example #5 在heredoc结构中使用双引号

echo  Hello World!
FOOBAR;
?> 

定界符 
1.PHP定界符的作用就是按照原样,包括换行格式什么的,输出在其内部的东西;
2.在PHP定界符中的任何特殊字符都不需要转义;
3.PHP定界符中的PHP变量会被正常的用其值来替换。

这个可以用来保存大段的文本比较有用。

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 Article Tags

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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles