Can I define a variable in a PHP if condition?

Viewed 53917

For example, can I do:

if ($my_array = wp_get_category($id)) {
    echo "asdf";
} else {
    echo "1234";
}

If nothing is returned by the function, I want to go into the else statement.

5 Answers

Following is one more alternative to define any variable (with safety):

$my_array = ($my_array = $wp_get_category($id)) ?: /* else statement here */;
Related