您的位置:首页 > 编程语言 > PHP开发

测试PHP的字符串类型(string)-Part 1

2009-04-22 15:03 246 查看
下面的代码都是在PHP命令行模式下执行的。

字符串可以用两个单引号定义。

php > echo 'a string defined with single quotes';
a string defined with single quotes

字符串可以分多行来定义。

php > echo 'a string defined with single quotes
php ' but with multiple lines
php ' did you notice it?';
a string defined with single quotes
but with multiple lines
did you notice it?

如果想在单引号定义的字符串里嵌入单引号,需要用反斜杠转义。

php > echo 'if you need to embed single quote in such a string, use slash. like /'';
if you need to embed single quote in such a string, use slash. like '

如果想在单引号定义的字符串里嵌入双引号,没有特别需要处理的。只要像使用一般字符一样就可以了。

php > echo 'if you need to embed double quote in such a string, just use it as other characters like "hello"';
if you need to embed double quote in such a string, just use it as other characters like "hello"

如果想在单引号定义的字符串里嵌入反斜杠,且反斜扛是最后一个字符,那就用两个反斜杠。
php > echo 'if backslash is the last character, then double it like//';
if backslash is the last character, then double it like/

如果想在单引号定义的字符串里嵌入反斜杠,而反斜扛不是最后一个字符,那可以用一个反斜杠或者两个反斜杠,其结果都是输出一个反斜杠。

php > echo 'if backslash is NOT the last character, then just use it like / or //.';
if backslash is NOT the last character, then just use it like / or /.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐