PHP 变量定义和变量替换的方法

2019-04-10 08:20:49于丽

Of having discovered sodium.
End_Of_Quote;
echo $clerihew;
Sir Humphrey Davy
Abominated gravy.
He lived in the odium
Of having discovered sodium.
<<<符号(我们习惯称为字符串定界符――译者注)告诉PHP解析器你正在书写一个heredoc。在<<<符号和标识符(本例中即 End_Of_Quote)之间必须有一个空格,这样程序才可以辨别标识符。从下一行开始就是被引用的文本,直到它遇到仅由标识符组成的一行为止。
你可以把分号放在终止标识符的后面来结束语句,正如前面的代码所示。如果你在一个更复杂的表达式中使用heredoc,你需要将表达式分行来写:
printf(<<< Template
%s is %d years old.
Template
, “Fred”, 35);
在heredoc中的单引号和双引号被跳过(当作一般的符号):
$dialogue = <<< No_More
“It's not going to happen!” she fumed.
He raised an eyebrow. “Want to bet?”
No_More;
echo $dialogue;
“It's not going to happen!” she fumed.
He raised an eyebrow. “Want to bet?”
在heredoc中的空白符也被保留:
$ws = <<< Enough
boo
hoo
Enough;
// $ws = ” boon hoon”;
因为在结尾终止符前的换行符将被移除,所以下面这两个赋值是相同的:
$s = ‘Foo';
// same as 和下面的相同
$s = <<< End_of_pointless_heredoc
Foo
End_of_pointless_heredoc;
如果想用一个换行符来结束heredoc引用的字符串,则需要自己额外加入:
$s = <<< End
Foo

End;
//注意Foo后面跟一个空行,不可删除
相关文章 大家在看