Can I define a variable in a PHP if condition?

前端 未结 5 2251
失恋的感觉
失恋的感觉 2020-12-05 23:17

For example, can I do:

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

If nothing is returned by t

5条回答
  •  忘掉有多难
    2020-12-05 23:53

    you might want something like this:

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

    Assuming the function returns null upon failure. You may have to adjust it a bit.

提交回复
热议问题