用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2535|回复: 1
收起左侧

分享下我写字符串界定符的经验,比较水

[复制链接]
发表于 2010-5-17 10:18:45 | 显示全部楼层 |阅读模式
字符界定符以<<<开始后面紧跟分隔符(可以自定义,通常全部大写,只能由字母、数字和下划线组成,但不能有空格,不能以数字开头)。
在初始分隔符之后,同一行里面不能有任何其他内容,甚至一个空格也不行!

在字符串的末尾使用同样的分隔符。结束分隔符必须位于单独一行的最前方,而且后面只能有一个分号。

这个语法非常的严格,如果语法不是100%的正确,通常都是些不经意的空格,就像我最开始一样,都会导致解析错误!
当然这种风格的字符串界定符好处也是不言而喻的。
希望对大家有所帮助。

这段代码就报错
PHP复制代码
<?php
echo<<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END
;
复制代码


但是只需要改一点点就好了
PHP复制代码
<?php
echo<<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END
;
 
复制代码
发表于 2010-6-8 23:35:05 | 显示全部楼层
phpwind坚持使用这种模式输出。目前来看,heredoc这种输出方式,也有很多可取之处。不过正如楼主所说,对程序员在写代码时的要求,也提高了不少。

本版积分规则