In PHP, "=>" is an array operator, used to correspond to the key value and key name in the array. The syntax is "array(key => value)"; "-> " is used to refer to methods and properties of class instances, and the syntax is "$obj->a".
The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.
1. The meaning of =>,->:
- > is used by objects to execute methods or obtain attributes.
=> is applied to key and value pairs in the array.
2. Usage
1. Usage of => The relationship between the key and value used in the array. For example:
$a = array('0' => '1','2' => '4',);echo $a['0'];echo $a['2'];
2. -> usage classes are used to refer to the methods and attributes of class instances. For example:
class Test{function add(){return $this->var++;}var $var = 0;}$a = new Test; //实例化对象名称 echo $a->add();echo $a->var;
Extended information
The -> code in PHP is as follows:
<?php class Car { public $speed = 0; //增加speedUp方法,使speed加10 public function speedUp(){ $this->speed+=10; } } $car = new Car(); $car->speedUp(); echo $car->speed; ?>
In PHP => The code is as follows:
<?php //从数组变量$arr中,读取键为apple的值 $arr = array('apple'=>"苹果",'banana'=>"香蕉",'pineapple'=>"菠萝"); $arr0=$arr["apple"]; if( isset($arr0) ) {print_r($arr0); } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What do => and -> mean in php. For more information, please follow other related articles on the PHP Chinese website!