php intval 和转换为 int - `(int) X` 之间有什么特别的区别吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1912599/
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
Is there any particular difference between intval and casting to int - `(int) X`?
提问by Sarfraz
Is there any particular difference between intval and (int)?
intval 和 (int) 之间有什么特别的区别吗?
Example:
例子:
$product_id = intval($_GET['pid']);
$product_id = (int) $_GET['pid'];
Is there any particular difference between above two lines of code?
以上两行代码有什么特别的区别吗?
采纳答案by Amber
intval()can be passed a basefrom which to convert. (int)cannot.
intval()可以传递一个基础,从中进行转换。(int)不能。
int intval( mixed $var [, int $base = 10 ] )
回答by t-dub
One thing to note about the difference between (int)and intval(): intval()treats variables which are already ints and floats as needing no conversion, regardless of the base argument (as of PHP 5.3.5 at least). This behavior isn't the most obvious, as noted in the comments on the PHP doc pageand shamelessly reiterated here:
关于(int)and之间区别的一件事要注意intval():intval()将已经int是 s 和floats 的变量视为不需要转换,无论基本参数如何(至少从 PHP 5.3.5 开始)。这种行为并不是最明显的,正如PHP 文档页面上的评论中所指出的,并在此处无耻地重申:
$test_int = 12;
$test_string = "12";
$test_float = 12.8;
echo (int) $test_int; // 12
echo (int) $test_string; // 12
echo (int) $test_float; // 12
echo intval($test_int, 8); // 12 <-- WOAH!
echo intval($test_string, 8); // 10
echo intval($test_float, 8) // 12 <-- HUH?
回答by Populus
Sorry for necroing, I just wanted to see if/how PHP7 has an effect on this question:
对不起,我只是想看看 PHP7 是否/如何对这个问题产生影响:
$ php -v
PHP 7.0.4-5+deb.sury.org~trusty+1 (cli) ( NTS )
The test:
考试:
php > $start_ts = microtime(true); for($i = 0; $i < 100000000; $i++) { $a = (int) '1'; } var_dump((microtime(true) - $start_ts)*1000 . ' ms');
string(18) "3279.1121006012 ms"
php > $start_ts = microtime(true); for($i = 0; $i < 100000000; $i++) { $a = intval('1'); } var_dump((microtime(true) - $start_ts)*1000 . ' ms');
string(18) "5379.3351650238 ms"
As you can see, casting is definitely faster, by almost100%
如您所见,投射速度绝对快了近100%
But I had to increase the loop count to 100 million before the difference was a matter of seconds, which is about when I would actually start caring about performance, in most cases.
但是我不得不将循环计数增加到 1 亿次,然后差异才几秒钟,这大约是我真正开始关心性能的时候,在大多数情况下。
So I'll stick with using the intvalfunction, because casting is a bit of language magic that's happening. Even if intvaluses casting behind the scenes, if there were to be a bug found with casting, and for some reason it could not be fixed (backwards compatibility?), then they could at least fix intvalto perform it's duty.
所以我会坚持使用这个intval函数,因为转换是一种正在发生的语言魔法。即使intval在幕后使用转换,如果在转换中发现错误,并且由于某种原因无法修复(向后兼容性?),那么他们至少可以修复intval以履行其职责。
Update (PHP 7.1 + Extra case):
更新(PHP 7.1 + 额外情况):
$ php -v
PHP 7.1.0RC6 (cli) (built: Nov 9 2016 04:45:59) ( NTS )
$ php -a
php > $start_ts = microtime(true); for($i = 0; $i < 100000000; $i++) { $a = (int) '1'; } var_dump((microtime(true) - $start_ts)*1000 . ' ms');
string(18) "3583.9052200317 ms"
php > $start_ts = microtime(true); for($i = 0; $i < 100000000; $i++) { $a = intval('1'); } var_dump((microtime(true) - $start_ts)*1000 . ' ms');
string(18) "3569.0960884094 ms"
php > $start_ts = microtime(true); for($i = 0; $i < 100000000; $i++) { $a = '1' + 0; } var_dump((microtime(true) - $start_ts)*1000 . ' ms');
string(18) "1641.7920589447 ms"
Looks like 7.1 optimized intval, and '1' + 0 is now the winner of this speed contest :) I'd still keep using intvalanyway
看起来像 7.1 优化intval,'1' + 0 现在是这次速度竞赛的获胜者 :)intval无论如何我仍然会继续使用
回答by Pascal MARTIN
回答by Kodos Johnson
One useful property of intvalis that - since it is a function and not a language construct - it can be passed as an argument to a function that expects a function. You can't do this with (int).
的一个有用的特性intval是——因为它是一个函数而不是一个语言结构——它可以作为一个参数传递给一个需要函数的函数。你不能用(int).
For example, I have used it to sanitize integers for inclusion into a SQL IN()clause by passing it to array_map. Here is an example:
例如,我使用它来清理整数以包含在 SQLIN()子句中,方法是将其传递给array_map. 下面是一个例子:
$ids = implode(",", array_map('intval', $_POST['array_of_integers']));
$sql = "SELECT * FROM table WHERE ids IN ($ids)";
回答by user900469
Amber is right and if I may add a useful information
type casting (adding a "(int)" before your expression ) is 300 to 600% faster than intval.
So if your purpose is not to dealing with other bases than decimal, I recommend using:
(int) $something
Amber 是对的,如果我可以添加一个有用的信息类型转换(在你的表达式之前添加一个“(int)”)比 intval 快 300% 到 600%。因此,如果您的目的不是处理十进制以外的其他基数,我建议使用:
(int) $something
回答by deceze
The thing that intvaldoes that a simple cast doesn't is base conversion:
这事情intval确实是一个简单的转换并不为基地的转换:
int intval ( mixed $var [, int $base = 10 ] )
If the base is 10 though, intvalshould be the same as a cast (unless you're going to be nitpicky and mention that one makes a function call while the other doesn't). As noted on the man page:
但是,如果基数为 10,则intval应该与强制转换相同(除非您要挑剔并提到一个调用函数而另一个不调用)。如手册页所述:
The common rules of integer casting apply.
整数转换的通用规则适用。

