Difference between -> and => in PHP

Difference between -> and => in PHP

0 8048


You might have seen -> or => in PHP, but not an idea of what these are called or what they actually mean. Right?

You might be wondering how can I predict your thoughts here? Isn't it? Actually I also faced the same problem. I was able to work perfectly with PHP but when it comes to defining anything particular, that time I was unable to take my stand. Thus, I decided I will learn each and every term which is not known commonly exactly like this one.

If you are a novice, you can learn PHP online to get your concept clear for the same.

Anyways, let's proceed to today's blog topic.

The double arrow operator =>

The arrays are accessed with the use of a double arrow operator. In other terms, the operator is also used to assign a certain value to an acceptable type of operator in the array index which can be in the form of either numeric or string-based (associative). Moreover, double arrow operator => assigns the value to an array key.

The object operator ->

The -> operator, also known as the object operator is used to access the properties and methods for a specific object. Besides, in simple words, the object operator -> is responsible for accessing an object method.

Moreover, if you are looking for a career opportunity you can search for PHP interview questions for getting more insights on how to answer interview questions. It will help you to understand about the difficulty level and you can prepare accordingly.

For a better understanding, please find the examples below with the outcomes for the same.

What does this mean in PHP =>

Answer: In PHP => is known as a double arrow operator or used as an associative array. This array is used to provide name keys. That means the named key will have an only value which you'll assign that associative array.

$states= array("Delhi"=>"07", "Haryana"=> "06", "Punjab"=>"03", "Goa"=>"030");
echo 'State code of Delhi is '.$states['Delhi'];

Here is the outcome

State code of Delhi is 07

Example 2:

// When it is used in a loop, then you can use it as:

foreach($states as $s =>$code){
    echo 'State name is '.$s.' and state code is '.$code;
   echo '<br>';

Here is the outcome

State name is Delhi and state code is 07

State name is Haryana and state code is 06

State name is Punjab and state code is 03

State name is Goa and state code is 030

For -> It's different from an associative array. It is called an object operator. It is time-consuming and easy to use. Just make a class and then define an object there. Let's see an example to understand it.

class StateClass
    public $object = 'State code of Delhi is 07';

$st = new StateClass();
echo $st->object;

Here is the outcome

State code of Delhi is 07

Note: While working with PHP, one must understand the following sort of entities that are Variables, objects and arrays.


That is all about today's blog. I hope you have gone through my previous blog on PHP operators, if not you can read that too for a better understanding. Moreover, you can also refer to the top 50 PHP interview questions that will help you to crack your interview effectively and efficiently.


Discount Coupons

Leave a Reply

    Waiting for your comments