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
- process a list of users in integers
- such that the value of each user is equal or greater than password
The above does not make sense to me.
An array in PHP is a map of keys to values:
If you want to do something with each key-value-pair in your array, you can use the
foreach
control structure: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:
$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
.=>
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:
Note that this can be used for numerically indexed arrays too.
Example:
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 theforeach
.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:
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:
Will get you this output:
(I'm using
var_dump
to generate a nice output, that facilitates debuging; to get a normal output, you'd useecho
)"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!
It means assign the key to $user and the variable to $pass
When you assign an array, you do it like this
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: