如果声明多个条件和括号

时间:2017-10-30 00:05:51

标签: php wordpress

我对php很新。我想知道如何在下面的陈述中检查后期元等于C1或C2

if ( $user_ID && $user_ID == $convert->post_author && get_post_meta( $project->ID,'et_payment_package', true) == 'C1')

这似乎不起作用

if ( ($user_ID && $user_ID == $convert->post_author && get_post_meta( $project->ID,'et_payment_package', true) == 'C1') || ($user_ID && $user_ID == $convert->post_author && get_post_meta( $project->ID,'et_payment_package', true) == 'C2') )

1 个答案:

答案 0 :(得分:2)

进行检查最简单,最干净的方法是拆分if语句。你不需要在一个if语句中进行整个检查:

// Check if $user_ID exists and it equals $convert->post author
if (!empty($user_ID) && $user_ID == $convert->post_author) {
    if (get_post_meta($project->ID, 'et_payment_package', true) == 'C1') {
        // run logic for C1
    }
    elseif (get_post_meta($project->ID, 'et_payment_package', true) == 'C2') {
        // run logic for C2
    }
}