首页 > PHP > PHP中变量定义和变量替换的方法

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

2009年06月23号
查看评论 发表评论 1,943次浏览

如果在用双引号括起来的字符串中发现一个未知的转义序列(例如,一个反斜杠后跟一个不在表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

类别PHP 标签
  1. Cheap Adidas Shoes
    发表于 2010年06月12号 17时46分34秒 | 1楼

    i hope so

  2. womens timberland boots sale
    发表于 2010年06月13号 15时26分45秒 | 2楼

    Thank you all!

  3. world cup jerseys 2010
    发表于 2010年06月17号 13时36分52秒 | 3楼

    your story is so beautiful.

  4. louis vuitton Monogram Mini Lin
    发表于 2010年06月18号 08时57分38秒 | 4楼

    Thanks for the laugh, I needed it!

  5. cheap basketball shoes
    发表于 2010年06月22号 09时09分05秒 | 5楼

    Motivation is the key.

  6. timberland boots
    发表于 2010年06月22号 09时56分58秒 | 6楼

    Even though it will not be here tomorrow, the effect of it will.

  7. sexy swimwear bikinis
    发表于 2010年06月22号 10时15分09秒 | 7楼

    ..so much appreciated…

  8. mbt chapa storm
    发表于 2010年06月23号 05时40分13秒 | 8楼

    our … thoughts are … synchronized

  9. Nike Air Force 1 Shoes
    发表于 2010年06月23号 09时28分03秒 | 9楼

    What a great happy colourful wallpaper you have – very cool.

  10. herve leger bandage
    发表于 2010年06月25号 02时19分19秒 | 10楼

    I want to hear more, his blog is pretty cool too.

  11. handbag
    发表于 2010年06月25号 04时51分51秒 | 11楼

    What a great happy colourful wallpaper you have – very cool.

  12. timberland boots sale
    发表于 2010年06月26号 17时47分25秒 | 12楼

    Ahhhhh! a very smart Heart!

  13. mens lacoste shoes
    发表于 2010年06月28号 07时16分14秒 | 13楼

    Cheers to you….I look forward to reading more of your work. What a gem you are.

  14. herve leger bandage
    发表于 2010年06月28号 09时31分27秒 | 14楼

    What else can one asked for that to inspire someone else? Very much appreciated

  15. Cheap Bags
    发表于 2010年06月28号 11时43分59秒 | 15楼

    I磎 so happy today :-) Feels like a lot of pieces has fallen into place.

评论页数:
1 2 3 4 255
你必需 登陆 才能发表评论.