[Explained]: What is the difference between => and -> in PHP?
  John Mwaniki /   22 Sep 2021

[Explained]: What is the difference between => and -> in PHP?

Most often, new PHP developers get confused between the usage of -> and => in programs. They wonder what is the difference between the two and the role and application of each in programs.

In this article, I will explain in detail the difference between the -> and => operator and demonstrate where each is applicable with aid of multiple code examples.

Before we jump into their differences and applications, let's first check what they have in common.

Both of them are operators.

An operator is a symbol that tells the compiler or interpreter to perform a certain operation such as an arithmetic calculation, comparison of two values, assignment of a value to a variable/constant, or traversal of an object among others.

What is => in PHP?

This is referred to as the double arrow operator. It is an assignment operator used in the creation of associative arrays.

It is placed in between the array key and its value. It assigns to the key (what is on its left enclosed quotes), the value of what is on its right side.

Syntax

"key" => value

Example

"name" => "John"

As from the example above, the key can be given any name but must be enclosed in quotes (single or double). The value on the right may, or may not be enclosed in quotes depending on its data type. For instance, you don't have to enclose an integer or a boolean value but must enclose for a string.

You can put the value directly or can store it in a variable or constant, then assign it to the key.

Key-value pairs are separated from each other with a comma (,) but you don't place a comma after the last pair.

Example 1

<?php
$person = array(
    "firstName" => "John",
    "lastName" => "Doe",
    "age" => 28,
    "gender" => "Male",
    "email" => "johndoe@gmail.com",
    "city" => "Germany"
);
?>

Example 2

You can also form the above array using the square ([ ]) brackets like below:

<?php
$person = [
    "firstName" => "John",
    "lastName" => "Doe",
    "age" => 28,
    "gender" => "Male",
    "email" => "johndoe@gmail.com",
    "city" => "Germany"
];
?>

What is -> in PHP?

This is referred to as the object operator, or sometimes the single arrow operator.

It is an access operator used for access/call methods and properties in a PHP object in Object-Oriented Programming (OOP).

Example

<?php
class People {

   public $name = 'John Doe';

   public function Developer() {

   }

}
$person = new People();
$person->Developer();
echo $person->name;
?>

The example above is object-oriented programming in PHP script.

We have created a "person" object of the class "People". The output of the code will be "John Doe".

We have accessed the public variable "$name" using the object operator on the person object.

We have also used the object operator to access the method "Developer".

In addition to accessing class objects, we can also use the object operator (->) to access the elements of a JSON object as in the example below:

<?php
$person = '{
    "firstName":"John",
    "lastName": "Doe",
    "age":28,
    "gender":"Male",
    "email":"johndoe@gmail.com",
    "city":"Germany"
}';
$data = json_decode($person);
echo $data->firstName;
?>

The output of this code will be "John". You can in the same way simply access the values of the other elements.

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 is placed in between the key and the value and assigns what is on its right(value) to what is on its left(key).

-> referred to as the object operator or single arrow operator. It is used for accessing the methods and properties of a class object. Is also used in accessing JSON object elements.