What is this PHP shortcode means? is_array($data) OR $data = (array) $data
What is this PHP code means ?
is_array($data) OR $data = (array) $开发者_StackOverflow中文版data
I see this code on PyroCMS
OR
is a short-circuit operator. If the left side is true
, the right side will not be evaluated. It basically says "if $data
is not an array, cast it to an array".
Note that this is rather redundant and could be abbreviated to:
$data = (array)$data;
This has the same effect. If it already is an array, casting to an array will do nothing, otherwise it will cast to an array.
It tests if $data is an array, and casts it to one if it isn't.
It is equal to:
if (!is_array($data)) $data = (array) $data
That code type casts $data to an array, if it's not an array.
More information on type casting - http://php.net/manual/en/language.types.type-juggling.php
It's a (terrible in my opinion) hackish way of writing:
if(!is_array($data)) {
$data = (array) $data;
}
Which is basically forcing the $data variable to be an array.
Don't write code like this. It's hard to process (mentally), and doesn't save any time. :-)
精彩评论