什么时候应该使用intval和何时int [英] When should one use intval and when int

查看:131
本文介绍了什么时候应该使用intval和何时int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么更好的选择:

// Option 1:
$intValue = (int) $numericValue;

// Option 2:
$intValue = intval($numericValue);

这两行之间是否有区别,在哪种情况下应使用哪一个?

Is there any difference between these two lines, and which should be used in which situation?

推荐答案

  1. intval比强制转换为int慢(这是一个函数)
  2. 如果
  3. intval的第一个参数是字符串,则可以接受它.
  1. intval is slower than casting to int (it's a function)
  2. intval can accept a base if its first parameter is a string

由于第二项确实很晦涩,因此客观上我们应该说intval简直是更糟".

Since the second item is really obscure, objectively we should say that intval is simply "worse".

但是,个人而言,我喜欢阅读intval($var)胜过(int)$var(后者可能还需要包装其他括号),并且由于如果您要在循环内转换为int,您肯定做错了什么,我使用intval.

However, personally I like reading intval($var) better than (int)$var (the latter may also require wrapping in additional parens) and since if you are converting to int inside a loop you are definitely doing something wrong, I use intval.

这篇关于什么时候应该使用intval和何时int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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