PHP 数组键需要用引号括起来吗? [英] Do PHP array keys need to we wrapped in quotes?

查看:41
本文介绍了PHP 数组键需要用引号括起来吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面哪个是正确的?第一个代码在 $_GET 数组中没有引号,第二个有,我知道当它是一串文本时你应该有它们,但在这种情况下它是一个变量,如果键是一个数字呢?

Which one below is correct? First code has no quotes in the $_GET array and the second one does, I know you are supposed to have them when it is a string of text but in this case it is a variable, also what about if the key is a number?

没有引号

function arg_p($name, $default = null) {
  return (isset($_GET[$name])) ? $_GET[$name] : $default;
}

带引号

function arg_p($name, $default = null) {
  return (isset($_GET['$name'])) ? $_GET['$name'] : $default;
}

推荐答案

第一个将使用 $name 的值作为键,而第二个将使用文字字符串 '$name' 作为键.

The first one will use the value of $name as key while the second will use the literal string '$name' as key.

这篇关于PHP 数组键需要用引号括起来吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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