PHP类型转换 [英] PHP typecasting

查看:149
本文介绍了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

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

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

All of them produce same result.

推荐答案

(int)$value

保存一个函数调用, code> 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.

当您需要使用 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天全站免登陆