PHP typecasting(PHP 类型转换)
问题描述
我想知道将值从一种类型转换为另一种类型的最佳方法是什么.
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 类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 类型转换
基础教程推荐
- PHP 类:全局变量作为类中的属性 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
