PHP中变量定义和变量替换的方法
如果在用双引号括起来的字符串中发现一个未知的转义序列(例如,一个反斜杠后跟一个不在表4-1中的字符),就忽略这个转义序列(如果警告级设置为E_NOTICE,就会为这样的未知序列产生一个警告):
$str = “What is \c this?”; // 未知的转义序列
echo $str ;
What is \c this?
4.1.4 字符串定界
Here Documents heredoc
使用heredoc可以简单地把多行字符串放在程序中,如下所示:
$clerihew = <<< End_Of_Quote
Sir Humphrey Davy
Abominated gravy.
He lived in the odium
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 = ” boo\n hoo\n”;
因为在结尾终止符前的换行符将被移除,所以下面这两个赋值是相同的:
$s = ‘Foo’;
// same as 和下面的相同
$s = <<< End_of_pointless_heredoc
Foo
End_of_pointless_heredoc;
如果想用一个换行符来结束heredoc引用的字符串,则需要自己额外加入:
$s = <<< End
Foo
End;
//注意Foo后面跟一个空行,不可删除
页面: 1 2
i hope so
Thank you all!
your story is so beautiful.
Thanks for the laugh, I needed it!
Motivation is the key.
Even though it will not be here tomorrow, the effect of it will.
..so much appreciated…
our … thoughts are … synchronized
What a great happy colourful wallpaper you have – very cool.
I want to hear more, his blog is pretty cool too.
What a great happy colourful wallpaper you have – very cool.
Ahhhhh! a very smart Heart!
Cheers to you….I look forward to reading more of your work. What a gem you are.
What else can one asked for that to inspire someone else? Very much appreciated
I磎 so happy today
Feels like a lot of pieces has fallen into place.