我想知道将值从一种类型转换为另一种类型的最佳方法是什么.
I was wondering what is the best way to typecast a value from one type to another.
我们应该使用哪种变体:
Which variant should we use:
intval($value);settype($value, 'int')(int)$value所有这些都产生相同的结果.
All of them produce same result.
(int)$value
与 intval($value) 和 settype($value, 'int') 相比,节省了一个函数调用.
saves one function call compares to intval($value) and settype($value, 'int').
而且 (int)$value 已经足够干净了,所以我更喜欢这种方式.
And (int)$value is clean enough, so I prefer this way.
When you need to use intval($value) 是当你需要使用指定的基数进行转换时(默认为基数 10).intval 接受第二个参数作为转换的基础.
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). intval accepts a second parameter for the base for the conversion.
这篇关于PHP 类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
PHP、MySQL PDOException 的死锁异常代码?Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死锁异常代码?)
PHP PDO MySQL 可滚动游标不起作用PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滚动游标不起作用)
PHP PDO ODBC 连接PHP PDO ODBC connection(PHP PDO ODBC 连接)
使用 PDO::FETCH_CLASS 和魔术方法Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔术方法)
php pdo 只从 mysql 获取一个值;等于变量的值php pdo get only one value from mysql; value that equals to variable(php pdo 只从 mysql 获取一个值;等于变量的值)
MSSQL PDO 找不到驱动程序MSSQL PDO could not find driver(MSSQL PDO 找不到驱动程序)