"="是什么意思?在PHP中意味着什么? [英] What does "=>" mean in PHP?

查看:76
本文介绍了"="是什么意思?在PHP中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

=>运算符在以下代码中是什么意思?

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

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

该代码是PHP.net上的注释. 用户未指定$user_list$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. 处理整数用户列表
  2. 使每个用户的值等于或大于密码

以上对我来说没有意义.

The above does not make sense to me.

推荐答案

=>是关联数组的分隔符.在该foreach循环的上下文中,它将数组的键分配给$user,并将值分配给$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.

示例:

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

示例:

$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

这篇关于"="是什么意思?在PHP中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆