Can I define a variable in a PHP if condition?

前端 未结 5 2255
失恋的感觉
失恋的感觉 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:47

    I found this wondering about the rules of declaring a variable then using it immediately in subsequent conditions in the same statement.

    Thanks to previous answer for the codepad link, I made my own to test the theory. Spoiler alert: It works.

    http://codepad.org/xTwzTwGR

提交回复
热议问题