在PHP关联数组索引中使用空格是否不好? [英] Is it a bad practice to use spaces in PHP associative array indexes?

查看:80
本文介绍了在PHP关联数组索引中使用空格是否不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用关联数组传递一些参数,例如:

I have to pass some parameters using an associative array, something like this:

$blockGroup['name=products type=complete']

做一些测试,我发现它可行,但这是一个不好的做法吗?可能会产生任何错误或意外行为吗?

Doing some tests i saw that it works, but is it a bad practice? Is it possible to generate any bug or unexpected behavior?

感谢任何建议!

编辑1

我在视图实现中使用此数组,完整结构为:

I am using this array in a view implementation, the complete structure is:

$blockGroup['name=products type=complete'][] =
    array(
        'name'  => 'GeForce',
        'value' => '99.99'
    );


推荐答案

不,不是。编程中的空格符号实际上并没有特殊含义。括在引号中并因此形成字符串的符号可以用作关联数组键。

No, it's not. Space symbol in programming doesn't really have a special meaning. Symbols enclosed in quotes and thus forming a string can be used as associative array keys.

实际上,很多时候将此类键用于关联数组会使

In fact, there are a lot of times when using such keys for associative arrays will make your code readable and handy to make changes to it.

$scores = array("John Doe" => 100, "Ivan Ivanovich" => 75.3);

我看到的是您试图使用数组键作为表达式,这确实是一个坏习惯。事情是为了他们的意图。使用关联键作为关联键。

What I see is you trying to use array keys as an expression, which is REALLY bad practice. Things are meant for what they meant for. Use associative keys as associative keys.

这篇关于在PHP关联数组索引中使用空格是否不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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