字符串php手册,PHP官方手册研读--数据类型:字符串

分享读PHP官方手册一点点心得;

五一闲置在家,撸代码之余,读读“拍黄片”官方手册,巩固下“拍黄片”基础(PHP);

一个字符串可以用 4 种方式表达:

单引号

双引号

heredoc 语法结构

nowdoc 语法结构

单引号

不会解析变量,要表达一个单引号自身,需在它的前面加个反斜线(\)来转义。要表达一个反斜线自身,则用两个反斜线(\)。转义字符不会被转义;

双引号

会解析变量,同样要表达一个双引号自身,需在它的前面加个反斜线(\)来转义。要表达一个反斜线自身,则用两个反斜线(\)。转义字符会被转义;

Heredoc 语法结构

解读:Heredoc会解析变量,Heredoc 结构就象是没有使用双引号的双引号字符串;

heredoc 句法结构: <<

注意:heredoc 结构中单双引号不用被转义, 结束时所引用的标识符必须在该行的第一列,而且,标识符的命名也要像其它标签一样遵守 PHP 的规则:只能包含字母、数字和下划线,并且必须以字母和下划线作为开头。

$name='ChenDasheng';

echo <<

My name is "$name" ;

EOT;

// My name is "ChenDasheng" ;

Nowdoc 结构

解读:Nowdoc不会解析变量,和Heredoc 结构恰恰相反,Nowdoc 结构是类似于单引号字符串的;

Nowdoc 句法结构: nowdoc 结构也用和 heredocs 结构一样的标记 <<

注意: Heredoc 结构的所有规则也同样适用于 nowdoc 结构,尤其是结束标识符的规则。

$name='ChenDasheng';

echo <<

My name is "$name" ;

EOT;

// My name is "$name" ;

字符串变量解析

当字符串用双引号或 heredoc 结构定义时,其中的变量将会被解析。

共有两种语法规则:一种简单规则,一种复杂规则。

简单的语法规则是最常用和最方便的,它可以用最少的代码在一个 string 中嵌入一个变量,一个 array 的值,或一个 object 的属性。

简单示例:

$juices = array("apple", "orange", "koolaid1" => "purple");

echo "He drank some $juices[0] juice.".PHP_EOL;

echo "He drank some $juices[1] juice.".PHP_EOL;

echo "He drank some juice made of $juice[0]s.".PHP_EOL; // Won't work

echo "He drank some $juices[koolaid1] juice.".PHP_EOL;

class people {

public $john = "John Smith";

public $jane = "Jane Smith";

public $robert = "Robert Paulsen";

public $smith = "Smith";

}

$people = new people();

echo "$people->john drank some $juices[0] juice.".PHP_EOL;

echo "$people->john then said hello to $people->jane.".PHP_EOL;

echo "$people->john's wife greeted $people->robert.".PHP_EOL;

echo "$people->robert greeted the two $people->smiths."; // Won't work

// He drank some apple juice.

// He drank some orange juice.

// He drank some juice made of s.

// He drank some purple juice.

// John Smith drank some apple juice.

// John Smith then said hello to Jane Smith.

// John Smith's wife greeted Robert Paulsen.

// Robert Paulsen greeted the two .

复杂(花括号)语法

复杂语法不是因为其语法复杂而得名,而是因为它可以使用复杂的表达式。

简单示例:

// 显示所有错误

error_reporting(E_ALL);

$great = 'fantastic';

// 无效,输出: This is { fantastic}

echo "This is { $great}";

// 有效,输出: This is fantastic

echo "This is {$great}";

echo "This is ${great}";

// 有效

echo "This square is {$square->width}00 centimeters broad.";

// 有效,只有通过花括号语法才能正确解析带引号的键名

echo "This works: {$arr['key']}";

// 有效

echo "This works: {$arr[4][3]}";

// 这是错误的表达式,因为就象 $foo[bar] 的格式在字符串以外也是错的一样。

// 换句话说,只有在 PHP 能找到常量 foo 的前提下才会正常工作;这里会产生一个

// E_NOTICE (undefined constant) 级别的错误。

echo "This is wrong: {$arr[foo][3]}";

// 有效,当在字符串中使用多重数组时,一定要用括号将它括起来

echo "This works: {$arr['foo'][3]}";

// 有效

echo "This works: " . $arr['foo'][3];

echo "This works too: {$obj->values[3]->name}";

echo "This is the value of the var named $name: {${$name}}";

echo "This is the value of the var named by the return value of getName(): {${getName()}}";

echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}";

// 无效,输出: This is the return value of getName(): {getName()}

echo "This is the return value of getName(): {getName()}";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值