php settype()int总是返回1 [英] php settype() int always returns 1
问题描述
我从 $ _ GET ['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");
然而,这总是返回1.它们是查询字符串中的数字,例如:
However, this always returns 1. They are numbers in the query string for example:
http://domain.com/example.php? category = 57
当我 echo $ category
时,它总是返回1.不管是什么?category =
背后有一个数字。
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!
推荐答案
settype
修改变量的类型。您正在使用它,好像它将返回一个新变量。
settype
modifies the type of the variable. You're using it as if it would return a new variable.
然而, settype $ c $如果类型更改成功,则返回true,否则返回false。你看到的结果是
1
,因为那是字符串表示的真实。
However, settype
returns true if the type change was successful, and false otherwise. You're seeing the result 1
since that's the string representation of true.
$category = (int) $_GET['category']; // or ...
$category = intval($_GET['category']);
这篇关于php settype()int总是返回1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!