PHP 类型转换 [英] PHP typecasting

查看:22
本文介绍了PHP 类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

所有这些都产生相同的结果.

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 类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆