" is used to refer to the attributes and methods of a class or to call functions in the class. The double arrow "=>" is used to define an array."/> " is used to refer to the attributes and methods of a class or to call functions in the class. The double arrow "=>" is used to define an array.">

Home  >  Article  >  Backend Development  >  The difference between single arrow and double arrow in php

The difference between single arrow and double arrow in php

尚
Original
2019-10-31 14:17:416555browse

The difference between single arrow and double arrow in php

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 &#39;a&#39;; } } $a=new a; $a->b(); 输出:a

=> Arrow, use to define an array:

$array1 = array(&#39;a&#39; = >5, &#39;b&#39; = >6);
while ($arrayitem = each($array1)) {
    extract($arrayitem);
    echo(&#39;<br />&#39;.$key.&#39;=&#39;.$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php7 cannot use mysqlNext article:php7 cannot use mysql