POST/GET 变量命名规则?
在命名表单中的 POST 变量或查询字符串中的 GET 变量时,是否需要遵循任何规则?
谢谢-
Are there any rules one needs to follow when naming POST variables in a form or GET variables in a query string?
Thanks-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从字面上回答这个问题,据我所知,在 php 中命名
$_POST
和$_GET
数组键确实没有“规则”。它是一个像其他数组一样的数组。看看Codepad 上的这个工作示例:对于表单输入名称,它们必须是合法的HTML“名称”属性(见下文)。然而,在实践中,许多不寻常的字符实际上会起作用。请记住,这并不意味着这是一个好主意。不同的服务器(可能还有不同的浏览器)对于某些字符(例如空格)的行为会有所不同。
正如 Tadeck 所指出的,重复的键将被覆盖阅读时的最后一个,但使用括号[]将在客户端通过将变量转换为数组来解决这个问题。
至于命名约定和最佳实践,没有太多空间。建议您坚持使用 AZ az 0-9、破折号和下划线。尽管 Ajay 建议使用数据库列名称作为表单输入名称为了方便起见,许多人会告诉您,向公众公开有关数据库的信息是不好的做法。我认为 invertedlambda 可能对这个问题有最接近的答案, 和 Tadeck 就最佳实践而言有最接近的答案。
关于 HTML“名称”属性: http://www.w3.org /TR/html4/types.html#h-6.2
也许有人可以启发我,上述文件是规则还是建议,我绝不是这方面的专家。在实践中,我似乎对违反其中一些规则没有任何问题。我也可以将此示例文档验证为严格的 XHTML:
将其粘贴到 验证器 中,它将经过。
要添加的另一个最佳实践是:使您的表单输入名称或 get/post 键有意义,当然就像所有其他命名约定一样。不要使用
input1
和$_GET['param']
。使用描述含义的名称,例如last_name
或$_GET['sort_order']
。TO answer the question literally, there really are no "rules" I'm aware of for naming
$_POST
and$_GET
array keys in php. It's an array like any other. Take a look at this working example on Codepad:In the case of form input names, they just have to be legal HTML "name" attributes (see below). However, in practice, a lot of unusual characters will actually work. Keep in mind that this doesn't mean it's a good idea. Different servers (and probably different browsers) will act differently with some characters like spaces for instance.
As Tadeck has noted, duplicate keys will be overwritten by the last one when reading, but using
brackets[]
will solve this on the client side by turning the variable into an array.As far as naming conventions and best practices, there isn't a lot of room. It's suggested that you stick to A-Z a-z 0-9, dashes, and underscores. Although Ajay has suggested using database column names for form input names as a matter of convenience, many people will tell you that it is bad practice to expose information about your database to the public. I think invertedlambda probably has the closest answer here to the question, and Tadeck has the closest answer as far as best practices.
Regarding HTML "name" attributes: http://www.w3.org/TR/html4/types.html#h-6.2
Maybe someone can enlighten me as to whether or not the above document is a rule or a recommendation, I'm by no means an expert on this subject. I seem to have no issues breaking some of these rules in practice. I also have no problem validating this example document as XHTML strict:
Paste it into the validator, it will pass.
One more best practice to add: Make your form input names or get/post keys meaningful, as with every other naming convention of course. Don't use
input1
and$_GET['param']
. Use names that describe the meaning, likelast_name
or$_GET['sort_order']
.我相信最好的解决方案是:
name="test[][]"< /code> 将在其他数组内的数组中创建值)并正确使用它,
?test=1&test=2
将产生问题,因为只有一个值会通过-更好地利用?test[]=1&test[]=2
因此将传递具有两个值的数组),此外,浏览您可能在 GitHub.com 因此您将使用经过许多人测试和使用的良好实践。
I believe the best solution is to:
name="test[][]"
will create value within array that is within other array) and use it properly,?test=1&test=2
will create problems as only one of the values will be passed - better use?test[]=1&test[]=2
so the array with two values will be passed),Furthermore, browse through different solutions that you may find on GitHub.com so you will be using practices that are good, tested and used by many people.
简而言之,不,只要您的变量名称符合 HTTP 规范和您正在使用的支持 Web 服务器,您就可以根据需要调用任何参数。
如果您使用与命名变量时相同的指导原则来命名参数,那么您应该能够选择一个好的名称。
In a word, no -- as long as your variable names are compliant with the HTTP specification and the backing web server that you're using, you can call your parameters anything you want.
If you use the same guidelines for naming a parameter as you use when you name variables, you should be able to pick a good name.
我们可以像变量名称规则一样使用名称,但也可以使用关键字作为 GET/POST 变量名称。
更好的是,我们可以在适用的情况下使用与数据库列名称相同的名称。但没有这样的规则。
这些只是一些建议。
We can use the names as like variable names rules , but can use the keywords also as GET/POST Variable names.
Better we can use the same names as database column names where ever applicable. But there is no rule like this.
Just these are some recommendations.