(int) $_GET['page'] 在 PHP 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7941545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does (int) $_GET['page'] mean in PHP?
提问by Simon Suh
I tried looking up (int)
but could only find documentation for the function int()
in the PHP manual.
我尝试查找,(int)
但只能int()
在 PHP 手册中找到该函数的文档。
Could someone explain to me what the above code does, and exactly how it works?
有人可以向我解释上面的代码是做什么的,以及它是如何工作的吗?
采纳答案by John
It convert (tries at least) whatever the value of the variable is to a integer. If there are any letter etc, in front it will convert to a 0.
无论变量的值是什么,它都将(至少尝试)转换为整数。如果有任何字母等,它会在前面转换为 0。
<?php
$var = '1a';
echo $var; // 1a
echo (int) $var; //1
$var2 = 'a2';
echo $var2; //a2
echo (int) $var2; // 0
?>
?>
回答by KingCrunch
You can find it in the manual in the section type juggling: type casting. (int)
casts a value to int and is a language construct, which is the reason that it looks "funny".
您可以在手册中的type juggling: type cast部分找到它。(int)
将值转换为 int 并且是一种语言结构,这就是它看起来“有趣”的原因。
回答by Griffin
(int)
converts a value to an integer.
(int)
将值转换为整数。
<?php
$test = "1";
echo gettype((int)$test);
?>
$ php test.php
integer
回答by Cas Bloem
Simple example will make you understand:
简单的例子会让你明白:
var_dump((int)8);
var_dump((int)"8");
var_dump((int)"6a6");
var_dump((int)"a6");
var_dump((int)8.9);
var_dump((int)"8.9");
var_dump((int)"6.4a6");
Result:
结果:
int(8)
int(8)
int(6)
int(0)
int(8)
int(8)
int(6)
回答by DaveRandom
What you are looking at there is known as type casting- for more information, see the manual page on type juggling.
您在那里看到的称为类型转换- 有关更多信息,请参阅有关类型杂耍的手册页。
The above piece of code casts (or converts) $_GET['page']
to an integer.
上面的一段代码将(或转换)$_GET['page']
为一个整数。
回答by Adam Wagner
In PHP, (int)
will cast the value following it to an int
.
在 PHP 中,(int)
将其后的值转换为int
.
Example:
例子:
php > var_dump((int) "5");
int(5)
I believe the syntax was borrowed from C.
我相信语法是从 C 借来的。
回答by Brian Glaz
this kind of syntax (int)
is called type casting. Basically it takes the variable following it and tries to force it into being an int
这种语法(int)
称为类型转换。基本上它需要跟随它的变量并试图迫使它成为一个int
回答by ant7
it casts the variable following it to integer. more info from documentation: http://php.net/manual/en/language.types.type-juggling.php
它将跟随它的变量转换为整数。来自文档的更多信息:http: //php.net/manual/en/language.types.type-juggling.php
Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast.
The casts allowed are:
- (int), (integer) - cast to integer
- (bool), (boolean) - cast to boolean
- (float), (double), (real) - cast to float
- (string) - cast to string
- (array) - cast to array (object) - cast to object
- (unset) - cast to NULL
PHP 中的类型转换与在 C 中的工作方式非常相似:所需类型的名称写在要转换的变量之前的括号中。
允许的演员表是:
- (int), (integer) - 转换为整数
- (bool), (boolean) - 转换为布尔值
- (float), (double), (real) - 转换为浮动
- (string) - 转换为字符串
- (array) - 转换为数组 (object) - 转换为对象
- (未设置) - 强制转换为 NULL