如果数组设置了,做什么?
我发布多个复选框,并将它们放入一个数组中 - 例如:“tags[]”
发布它们时,我用逗号将它们内爆。
如果在表单上没有检查任何标签,然后发布,我会收到错误,因为脚本试图破坏不存在的东西。
我尝试过使用这样的东西:
if (isset($_POST['tags'])){
$tags = implode(", ", noescape($_POST['tags']));
}
检查它是否存在然后将其内爆的最佳方法是什么?
isset,array_key_存在吗?
I am posting multiple checkboxes, and putting them into an array - for example: "tags[]"
When posting them, I am imploding them with commas.
If NO tags are checked on the form, and then posted, I get errors as the script is trying to implode something that isn't there.
I have tried using something like this:
if (isset($_POST['tags'])){
$tags = implode(", ", noescape($_POST['tags']));
}
What is the best way to check if it exists, then implode it?
isset, array_key_exists?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以在一行中完成此操作,在这种情况下
isset
和array_key_exist
会给您相同的结果,但您可能需要检查$_POST['tags ']
是一个数组...或者
您可以在这里测试:http://codepad.org/XoU4AdsJ
You could do it in one line, in this situation
isset
andarray_key_exist
would give you the same result but then you may want to check if$_POST['tags']
is an array...or
You can test here : http://codepad.org/XoU4AdsJ
那应该有效:
That should work:
我只会在内爆之前使用 is_array ,这样你的内爆只有在你内爆的情况下才有效var 是一个现有的数组。如果未设置则返回 0 :)
http://php.net/手册/en/function.is-array.php
I would just use is_array before imploding so your implode only works if your imploded var is an existing array. Returns 0 if it is not set as well :)
http://php.net/manual/en/function.is-array.php
实际上,更简单的方法是执行以下操作:
然后删除默认值。
显然,如果某些恶意用户决定在没有任何数据的情况下向您的脚本发送帖子,这仍然会导致错误。
Actually, an easier way to do this would be to do something like this:
And then remove the default value.
Obviously this would still cause errors if some malicious user decided to send a post to your script without any data at all.
我会使用 is_array() 和 count():
I'd use is_array() and count():