0

In php/wordpress I have made a function. I want to pass some parameteres inside the function so that it will show result according to that. So far now my function code is like this

$user_id = get_current_user_id();
function check_user_access($role, $action = NULL ) {
    if( $role == 'subscriber') {
        if( $action = 'check_customer' ) {
            $check_customer = $wpdb->get_var("SELECT COUNT(id) FROM `table1` WHERE `user_id` = $user_id");
            return $check_customer;
        }

        if( $action = 'check_users' ) {
            $check_users = $wpdb->get_var("SELECT COUNT(id) FROM `table2` WHERE `user_id` = $user_id");
            return $check_users;
        }
    }
}

Now I am using this function like this

$role = 'subscriber';
$check_customers = check_user_access($role, $action = 'check_users' );
if( $check_users <=1 ) {
    //do something;
}
if( $check_users > 1 ) {
    //do something other;
}

But its showing the result of $action = 'check_customer'. Means its working for the first block condition. Can someone tell me how to solve this? Am I doing something wrong?

5
  • I'm not sure what the issue is? Commented Jul 16, 2015 at 13:30
  • 4
    You're using = instead of ==, which means you're assigning the value instead of comparing it. The result of an assignment is always true, so your first nested if condition will always pass. Commented Jul 16, 2015 at 13:32
  • @David I think it should be $action == 'check_customer' ? Commented Jul 16, 2015 at 13:34
  • @NewUser: Yes, use == for comparison. Just like you do in your first if statement. Commented Jul 16, 2015 at 13:34
  • I don't really like reading Yoda Conditions but I can kinda see the point - they sidestep this issue completely... en.wikipedia.org/wiki/Yoda_conditions Commented Jul 16, 2015 at 13:35

1 Answer 1

2

change your

 if( $action = 'check_customer' ) {}

to

if( $action == 'check_customer' ) {}

= means Assignment Operator

== means Comparison Operator

refer - from here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.