Conclusion. The two operators, => and -> may look similar but are totally different in their usage. => is referred to as double arrow operator. It is an assignment operator used in associative arrays to assign values to the key-value pairs when creating arrays.
It means assign the key to $user and the variable to $pass. When you assign an array, you do it like this. $array = array("key" => "value"); It uses the same symbol for processing arrays in foreach statements. The '=>' links the key and the value.
-> and => are both operators. The difference is that => is the assign operator that is used while creating an array. For example: array(key => value, key2 => value2) And -> is the access operator. It accesses an object's value.
Arrow functions are introduced as an update in the PHP version 7.4. Arrow functions are supposed to be a more concise version of anonymous functions. Arrow functions can be seen as shorthand functions that automatically inherit the parent scope's variables.
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();
->
is used to call a method, or access a property, 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);
And since PHP 7.4+ the operator =>
is used too for the added arrow functions, a more concise syntax for anonymous functions.
=>
is used in associative array key value assignment. Take a look at:
http://php.net/manual/en/language.types.array.php.
->
is used to access an object method or property. Example: $obj->method()
.
->
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With