这在 PHP 中是什么意思 -> 或 =>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14037290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:29:19  来源:igfitidea点击:

What Does This Mean in PHP -> or =>

phpsyntax

提问by Willy Keatinge

Possible Duplicate:
where we use object operator “->” in php
Reference - What does this symbol mean in PHP?

可能的重复:
我们在 php参考中使用对象运算符“->”的地方
- 这个符号在 PHP 中是什么意思?

I see these in PHP all the time but I don't have a clue as to what they actually mean. What does ->do and what does =>do. And I'm not talking about the operators. They're something else, but nobody seems to know...

我一直在 PHP 中看到这些,但我不知道它们的实际含义。做->什么和做=>什么。我不是在谈论运营商。他们是别的东西,但似乎没有人知道......

回答by zafus_coder

The double arrow operator, =>, is used as an access mechanism for arrays. This means that what is on the left side of it will have a corresponding value of what is on the right side of it in array context. This can be used to set values of any acceptable type into a corresponding index of an array. The index can be associative (string based) or numeric.

双箭头操作者=>被用作用于阵列的访问机制。这意味着它左侧的内容将具有数组上下文中右侧内容的相应值。这可用于将任何可接受类型的值设置为数组的相应索引。索引可以是关联的(基于字符串的)或数字的。

$myArray = array(
    0 => 'Big',
    1 => 'Small',
    2 => 'Up',
    3 => 'Down'
);

The object operator, ->, is used in object scope to access methods and properties of an object. It's meaning is to say that what is on the right of the operator is a member of the object instantiated into the variable on the left side of the operator. Instantiated is the key term here.

对象操作者->被在对象范围内用于访问的方法和对象的属性。意思是说运算符右边的是实例化为运算符左边变量的对象的成员。实例化是这里的关键术语。

// Create a new instance of MyObject into $obj
$obj = new MyObject();
// Set a property in the $obj object called thisProperty
$obj->thisProperty = 'Fred';
// Call a method of the $obj object named getProperty
$obj->getProperty();

回答by DWright

=>is used in associative array key value assignment. Take a look at:

=>用于关联数组键值分配。看一眼:

http://php.net/manual/en/language.types.array.php.

http://php.net/manual/en/language.types.array.php

->is used to access an object method or property. Example: $obj->method().

->用于访问对象方法或属性。例子:$obj->method()

回答by Sivagopal Manpragada

->is used to call a method on the object of a class

->用于调用类对象上的方法

=>is used to assign values to the keys of an array

=>用于为数组的键赋值

E.g.:

例如:

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34, 1=>2); 

回答by Samuel Cook

->

->

calls/sets object variables. Ex:

调用/设置对象变量。前任:

$obj = new StdClass;
$obj->foo = 'bar';
var_dump($obj);

=>Sets key/value pairs for arrays. Ex:

=>为数组设置键/值对。前任:

$array = array(
    'foo' => 'bar'
);
var_dump($array);