小编Ste*_*314的帖子

转换为数组VS is_array()

有没有人知道任何问题,性能或其他问题,可以通过将变量转换为数组而不是先检查它来实现?

// $v could be a array or string
$v = array('1','2','3'); 
Run Code Online (Sandbox Code Playgroud)

要么

$v = '1';
Run Code Online (Sandbox Code Playgroud)

代替:

if (is_array($v)) foreach ($v as $value) {/* do this */} else {/* do this */}
Run Code Online (Sandbox Code Playgroud)

我已经开始使用:

foreach((array) $v as $value) {
    // do this
}
Run Code Online (Sandbox Code Playgroud)

它会停止重复代码 - 但性能是我的想法,而不是丑陋的代码.

另外,有谁知道php如何处理将数组转换为数组?没有抛出任何错误,但是php引擎是否检查它是否为数组,然后在执行转换过程之前返回结果?

php arrays performance casting

5
推荐指数
1
解决办法
3377
查看次数

标签 统计

arrays ×1

casting ×1

performance ×1

php ×1