Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "=>" mean in PHP?

Tags:

php

What does the => operator mean in the following code?

foreach ($user_list as $user => $pass)

The code is a comment at PHP.net. The user does not specify the value of $user_list, $user or $pass. I normally see that => means equal or greater than.

However, I am not sure about its purpose here because it is not assigned. I read the code as

  1. process a list of users in integers
  2. such that the value of each user is equal or greater than password

The above does not make sense to me.

like image 415
Léo Léopold Hertz 준영 Avatar asked Aug 06 '09 22:08

Léo Léopold Hertz 준영


People also ask

What is the => in PHP?

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.

What does this -> variable means in PHP?

$this is reference to a PHP Object that was created by the interpreter for you, that contains an array of variables. If you call $this inside a normal method in a normal class, $this returns the Object (the class) to which that method belongs.

What is arrow in PHP?

Arrow functions are only available in PHP versions 7.4 and up. Arrow functions have access to all variables from the scope in which they were created. The general syntax of an arrow function is: fn(arguments) => expression to be returned; ❮ PHP Keywords.

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


6 Answers

=> is the separator for associative arrays. In the context of that foreach loop, it assigns the key of the array to $user and the value to $pass.

Example:

$user_list = array(
    'dave' => 'apassword',
    'steve' => 'secr3t'
);

foreach ($user_list as $user => $pass) {
    echo "{$user}'s pass is: {$pass}\n";
}
// Prints: 
// "dave's pass is: apassword"
// "steve's pass is: secr3t"

Note that this can be used for numerically indexed arrays too.

Example:

$foo = array('car', 'truck', 'van', 'bike', 'rickshaw');
foreach ($foo as $i => $type) {
    echo "{$i}: {$type}\n";
}
// prints:
// 0: car
// 1: truck
// 2: van
// 3: bike
// 4: rickshaw
like image 191
hobodave Avatar answered Oct 05 '22 08:10

hobodave


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.

According to the PHP Manual, the '=>' created key/value pairs.

Also, Equal or Greater than is the opposite way: '>='. In PHP the greater or less than sign always goes first: '>=', '<='.

And just as a side note, excluding the second value does not work like you think it would. Instead of only giving you the key, It actually only gives you a value:

$array = array("test" => "foo");

foreach($array as $key => $value)
{
    echo $key . " : " . $value; // Echoes "test : foo"
}

foreach($array as $value)
{
    echo $value; // Echoes "foo"
}
like image 23
Tyler Carter Avatar answered Oct 05 '22 07:10

Tyler Carter


Code like "a => b" means, for an associative array (some languages, like Perl, if I remember correctly, call those "hash"), that 'a' is a key, and 'b' a value.

You might want to take a look at the documentations of, at least:

  • foreach
  • arrays

Here, you are having an array, called $user_list, and you will iterate over it, getting, for each line, the key of the line in $user, and the corresponding value in $pass.

For instance, this code:

$user_list = array(
    'user1' => 'password1',
    'user2' => 'password2',
);

foreach ($user_list as $user => $pass)
{
    var_dump("user = $user and password = $pass");
}

Will get you this output:

string 'user = user1 and password = password1' (length=37)
string 'user = user2 and password = password2' (length=37)

(I'm using var_dump to generate a nice output, that facilitates debuging; to get a normal output, you'd use echo)


"Equal or greater" is the other way arround: "greater or equals", which is written, in PHP, like this; ">="
The Same thing for most languages derived from C: C++, JAVA, PHP, ...


As a piece of advice: If you are just starting with PHP, you should definitely spend some time (maybe a couple of hours, maybe even half a day or even a whole day) going through some parts of the manual :-)
It'd help you much!

like image 26
Pascal MARTIN Avatar answered Oct 05 '22 08:10

Pascal MARTIN


An array in PHP is a map of keys to values:

$array = array();
$array["yellow"] = 3;
$array["green"] = 4;

If you want to do something with each key-value-pair in your array, you can use the foreach control structure:

foreach ($array as $key => $value)

The $array variable is the array you will be using. The $key and $value variables will contain a key-value-pair in every iteration of the foreach loop. In this example, they will first contain "yellow" and 3, then "green" and 4.

You can use an alternative notation if you don't care about the keys:

foreach ($array as $value)
like image 25
Scharrels Avatar answered Oct 05 '22 09:10

Scharrels


Arrays in PHP are associative arrays (otherwise known as dictionaries or hashes) by default. If you don't explicitly assign a key to a value, the interpreter will silently do that for you. So, the expression you've got up there iterates through $user_list, making the key available as $user and the value available as $pass as local variables in the body of the foreach.

like image 39
Meredith L. Patterson Avatar answered Oct 05 '22 08:10

Meredith L. Patterson


$user_list is an array of data which when looped through can be split into it's name and value.

In this case it's name is $user and it's value is $pass.

like image 21
Mizu Avatar answered Oct 05 '22 08:10

Mizu