Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of => in PHP

Tags:

php

What does this mean in PHP and when is the time to use it?

 =>

Another example.

 foreach ($parent as $task_id => $todo)
like image 303
sawu Avatar asked Oct 31 '09 19:10

sawu


People also ask

What does => mean in PHP?

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.

What is the difference between -> and => in PHP?

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.

What does => mean in laravel?

-> 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.

What is -> used for?

It is used with a pointer variable pointing to a structure or union. The arrow operator is formed by using a minus sign, followed by the greater than symbol as shown below. Operation: The -> operator in C or C++ gives the value held by variable_name to structure or union variable pointer_name.


3 Answers

To elaborate a bit on what has already been said.

Assuming that you know about arrays in PHP. Which is really a way of grouping a "list" of items under the same variable given a certain index - normally a numeric integer index starting from 0. Say we want to make a list of the indexes English term, that is,

Zero
One
Two
Three
Four
Five

Representing this in PHP using an array could be done like so:

$numbers = array("Zero", "One", "Two", "Three", "Four", "Five");

Now, what if we wanted the reverse situation? Having "Zero" as key and 0 as value? Having a non-integer as a key of an array in PHP is called an associative array where each element is defined using the syntax of "key => value", so in our example:

$numbers = array("Zero" => 0, "One" => 1, "Two" => 2, "Three" => 3, "Four" => 4, "Five" => 5);

The question now becomes: What if you want both the key and the value when using a foreach statement? Answer: the same syntax!

$numbers = array("Zero" => 0, "One" => 1, "Two" => 2, "Three" => 3, "Four" => 4, "Five" => 5);

foreach($numbers as $key => $value){
    echo "$key has value: $value\n";
}

This would display

Zero has value: 0
One has value: 1
Two has value: 2
Three has value: 3
Four has value: 4
Five has value: 5
like image 183
kastermester Avatar answered Oct 29 '22 19:10

kastermester


It is used to create an associative array like this:

$arr = array( "name" => "value" );

And also in a foreach loop like this:

foreach ($arr as $name => $value) {
    echo "My $name is $value";
}
like image 44
Ramon Avatar answered Oct 29 '22 19:10

Ramon


You can use it working with arrays:

array ("key" => "value", "key" => "value")

... or in a foreach statement:

foreach ($my_array as $key => $value)
...
like image 43
Pekka Avatar answered Oct 29 '22 19:10

Pekka