与数组相比,使用 parse_str 作为可选函数参数有优势吗?
我碰巧对 WordPress 博客进行了一些更改,并注意到他们使用 parse_str
(http:// /php.net/parse_str)用于解析并将其可选参数设置为函数。
我想知道这比发送数组是否有优势?
示例:
使用数组:
$blahOptions = array(
'option_1' => true,
);
BlahArray($blahOptions);
function BlahArray($options = array()) {
$defaults = array(
'option_1' => false,
'option_2' => 'blah',
);
// this would probably be in a function to be used everywhere
foreach ($defaults as $defaultOption => $defaultValue) {
if (!isset($options[$defaultOption])) $options[$defaultOption] = $defaultValue;
}
}
使用 parse_str
:
$blahOptions = 'option_1=1';
BlahString($blahOptions);
function BlahString($options = '') {
$defaults = array(
'option_1' => false,
'option_2' => 'blah',
);
parse_str($options, $defaults);
$options = $defaults;
}
I happened to be making some changes to a WordPress blog and noticed that they use parse_str
(http://php.net/parse_str) for parsing and setting their optional parameters to a function.
I'm wondering if there is an advantage to this over sending an array?
Examples:
With array:
$blahOptions = array(
'option_1' => true,
);
BlahArray($blahOptions);
function BlahArray($options = array()) {
$defaults = array(
'option_1' => false,
'option_2' => 'blah',
);
// this would probably be in a function to be used everywhere
foreach ($defaults as $defaultOption => $defaultValue) {
if (!isset($options[$defaultOption])) $options[$defaultOption] = $defaultValue;
}
}
With parse_str
:
$blahOptions = 'option_1=1';
BlahString($blahOptions);
function BlahString($options = '') {
$defaults = array(
'option_1' => false,
'option_2' => 'blah',
);
parse_str($options, $defaults);
$options = $defaults;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 parse_str,您可以从 URL 中放入查询字符串,并且该函数将起作用。 如果您使用数组,并且想要使用查询字符串,则必须在调用函数之前将所有内容枚举到数组中。
我并不完全相信这是最好的方法,但我看到它如何增加一点灵活性。
With parse_str, you can potentially drop in the query string from the URL, and the function will work. If you use an array, and you want to use the query string, you'll have to enumerate everything into an array before calling the function.
I'm not totally convinced it's the best way to go, but I see how it can add a bit of flexibility.
不,这似乎是一种传递函数参数参数的荒谬方式。 如果您需要重新创建 $_GET 或 $_POST 变量或类似的内容,我可以理解,但是对于函数的参数? 这就是代码味道。
他们应该使用数组,然后使用 extract() 代替。 我以前使用过 Wordpress,我的建议是与代码保持一定的距离。 它不是模型编程的示例。
No. That seems like a ridiculous way to pass functional parameter arguments. I could understand it if you needed to recreate $_GET or $_POST variables or something along those lines, but for parameters to a function? That's code smell right there.
They should be using an array, and then utilizing extract() instead. I've worked with Wordpress before, and my advice is to keep the code at arm's length. It is not an example of model programming.
不,缺点多于优点。
当您使用单个字符串时,您只能传递字符串值。 通过数组,您可以使用每种 PHP 数据类型,并且每个元素的值类型彼此独立。
No. There are more disadvantages than advantages.
When you’re using a single string, you just can pass string values. With an array you can use every PHP data type and every element’s value type is independently of each other.