The difference between single arrow and double arrow in php:
Use the -> symbol to reference the properties and methods of a class.
The following is an example applet:
<?php //定义类Cart class Cart { var $items; // 购物车中的物品 // 将 $num 个 $artnr 物品加入购物车 function add_item($artnr, $num) { $this->items[$artnr] += $num; } // 将 $num 个 $artnr 物品从购物车中取出 function remove_item($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } elseif ($this->items[$artnr] == $num) { unset($this->items[$artnr]); return true; } else { return false; } } } //示例继承定义类Named_Cart class Named_Cart extends Cart { var $owner; function set_owner ($name) { $this->owner = $name; } } //使用类的代码 $ncart = new Named_Cart; // 新建一个有名字的购物车 $ncart->set_owner("kris"); // 给该购物车命名 print $ncart->owner; // 输出该购物车主人的名字 $ncart->add_item("10", 1); // (从购物车类中继承来的功能) ?>
"->" This arrow can also be used to call a function in the class
class a { function b() { echo 'a'; } } $a=new a; $a->b(); 输出:a
=> Arrow, use to define an array:
$array1 = array('a' = >5, 'b' = >6); while ($arrayitem = each($array1)) { extract($arrayitem); echo('<br />'.$key.'='.$value); } 输出:a = 5 b = 6
Summary: PHP single arrow "->" is used to reference the attributes and methods of a class or call functions in the class. The double arrow "=>" is used to define an array.
Recommended: php server
The above is the detailed content of The difference between single arrow and double arrow in php. For more information, please follow other related articles on the PHP Chinese website!