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

The difference between single arrow and double arrow in php

Release: 2023-02-28 08:24:01
Original
6641 people have browsed it

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);  // (从购物车类中继承来的功能)
?>
Copy after login

"->" 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
Copy after login

=> 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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template