PHP中“=>
PHP的"=>"符号
在PHP中"=>"符号是用于在数组中分配键值对,主要在关联数组中使用。【视频教程推荐:PHP教程】
基本句式:
key => value
说明:"=>"符号的左侧值称为键,右侧值称为键值。
“=>"符号的使用示例
下面通过简单代码示例来介绍"=>"符号的使用方法。
示例1:使用“=>”符号创建关联数组
<?php header("content-type:text/html;charset=utf-8"); $subject = array( "Maths" => 95, "Physics" => 90, "Chemistry" => 96, "English" => 93, "Computer" => 98 ); echo "学生成绩:<br><br>"; echo "数学:" . $subject["Maths"], "<br>"; echo "物理:" . $subject["Physics"], "<br>"; echo "化学:" . $subject["Chemistry"], "<br>"; echo "英语:" . $subject["English"], "<br>"; echo "计算机:" . $subject["Computer"], "<br>"; ?>
输出:
示例2:使用“=>”符号创建数字索引数组
<?php $arr = array( "0" => 7, "1" => 10, "2" => 8, "3" => 5 ); foreach($arr as $key => $value) { echo $key . " => " . $value . "<br>"; } ?>
输出:
示例3:用于在不使用“=>”符号的情况下分配数字索引
<?php header("content-type:text/html;charset=utf-8"); $name = array("Zack", "Anthony", "Ram", "Salim", "Raghav"); foreach($name as $key => $value) { echo $key . " => " . $value . "<br>"; } ?>
输出:
赞 (0)