PHP 类型转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11857052/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:17:05  来源:igfitidea点击:

PHP typecasting

phptypescasting

提问by s.webbandit

I was wondering what is the best way to typecast a value from one type to another.

我想知道将值从一种类型转换为另一种类型的最佳方法是什么。

Which variant should we use:

我们应该使用哪个变体:

  1. intval($value);
  2. settype($value, 'int')
  3. (int)$value
  1. intval($value);
  2. settype($value, 'int')
  3. (int)$value

All of them produce same result.

它们都产生相同的结果。

回答by xdazz

(int)$value

saves one function call compares to intval($value)and settype($value, 'int').

intval($value)和相比,节省了一个函数调用settype($value, 'int')

And (int)$valueis clean enough, so I prefer this way.

而且(int)$value足够干净,所以我更喜欢这种方式。

When you need to use intval($value)is that when you need to use the specified base for the conversion (the default is base 10). intvalaccepts a second parameter for the base for the conversion.

当您需要使用intval($value)时,您需要使用指定的基数进行转换(默认为基数 10)。intval接受第二个参数作为转换的基数。

回答by raidenace

I prefer using

我更喜欢使用

settype($value,getType(intval((int)$value)));

回答by Michael Fenwick

The answer here is to use whatever reads "cleaner" to you. Any difference in speed is going to be so minor, that worrying about it is almost certain to cost you more time than you are liable to save. What will save time, however, is having code that you can read and understand in the future.

这里的答案是使用对您来说“更清洁”的任何内容。速度上的任何差异都将非常小,以至于担心它几乎肯定会花费您比您可以节省的更多时间。然而,节省时间的是拥有您将来可以阅读和理解的代码。

There's an excellent article explain this very thing at Coding Horror.

Coding Horror 上有一篇很棒的文章解释了这一点。

回答by Igor Evstratov

(int)$valueis much faster then other ways

(int)$value比其他方式快得多

回答by Deji

It can depend on what types you're converting. PHP is already designed to convert types on-the-fly. It can convert the string '5' into the integer value 5 or float 5.0 if necessary. That's just PHP's natural type converting, but there are ways to force similar behaviours.

这取决于您要转换的类型。PHP 已经被设计为即时转换类型。如有必要,它可以将字符串 '5' 转换为整数值 5 或浮点数 5.0。这只是 PHP 的自然类型转换,但有一些方法可以强制执行类似的行为。

In many cases intval() can actually be faster than (int) typecasting, especially in string-to-integer converting. But personally, as I also write C++, I use typecasting as it is more natural and neat to me. The results, however, vary slightly in different situations. I never thought of settype() to be promising, though.

在许多情况下,intval() 实际上比 (int) 类型转换更快,尤其是在字符串到整数的转换中。但就我个人而言,因为我也写 C++,所以我使用类型转换,因为它对我来说更自然和整洁。然而,结果在不同情况下略有不同。不过,我从没想过 settype() 很有前途。

回答by Elangovan

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.

PHP 中的类型转换与在 C 中的工作方式非常相似:所需类型的名称写在要转换的变量之前的括号中。

http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting