php settype() int 总是返回 1
我从 $_GET['category']
获取一个变量。这始终应该是一个整数。因此,当我设置变量时,我使用:
$category=settype($_GET['category'], "int");
// Also tried
$category=settype($_GET['category'], "integer");
但是,这总是返回 1。它们是查询字符串中的数字,例如:
http://domain.com/example.php?category=57
当我 echo $category
时,它总是返回 1。无论什么 ?category=后面有一个数字。
任何帮助将不胜感激!谢谢你!
I am getting a variable from $_GET['category']
. This is always supposed to be a integer. So when I set the variable I use:
$category=settype($_GET['category'], "int");
// Also tried
$category=settype($_GET['category'], "integer");
However, this always returns 1. They are numbers in the query string for example:
http://domain.com/example.php?category=57
When I echo $category
it always returns 1. No mater what ?category=
has behind it for a number.
Any help would be appreciated! Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
settype
修改变量的类型。您使用它就好像它会返回一个新变量。但是,如果类型更改成功,
settype
返回 true,否则返回 false。您将看到结果1
,因为这是 true 的字符串表示形式。您应该使用强制转换,或者intval:
settype
modifies the type of the variable. You're using it as if it would return a new variable.However,
settype
returns true if the type change was successful, and false otherwise. You're seeing the result1
since that's the string representation of true.You should either use casting, or intval:
它不返回值,只返回设置类型是否成功。您的
$_GET['category']
变量现在是一个int
。如果您想要执行返回值但保持变量不变的强制转换,语法为:It does not return the value, it just returns whether setting the type was successful or not. Your
$_GET['category']
variable is now anint
. If you want to do a cast which returns the value but leaves the variable untouched, the syntax is:请阅读
settype()
的文档。仅供参考,它的返回值为bool
。相反,尝试铸造,例如
Please read the documentation for
settype()
. FYI, its return value isbool
.Instead, try casting, eg
请正确阅读文档。
第一个参数是按引用传递。所以不需要在另一个变量中设置返回值。
你就是喜欢,
谢谢。
Please read documentation properly.
The first argument is pass by reference. So no need to set return value in another variable.
You just do like,
Thanks.