Title: if / elseif Logic Error
Last modified: August 20, 2016

---

# if / elseif Logic Error

 *  Resolved [theslink2000](https://wordpress.org/support/users/theslink2000/)
 * (@theslink2000)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/if-elseif-logic-error/)
 * Hi, I was hoping someone could point out where on earth my logic is falling apart
   in this nested if statement.
 * This is a form of security for my site, basically it’s limiting what each user
   can see within their roles, the actual implementation is a little complicated
   without explaining the site in great detail but I can say that I’ve thoroughly
   checked and the variables work in the correct locations.
 * Here’s where I’m at:
 *     ```
       add_action('pre_get_posts', 'view_my_records');
   
       function view_my_records($q) {
       	if(is_user_logged_in()){
   
       		if(in_array('patient', $types)){
       			if($role == 'patient'){
       				if($path != $user_check){
       					wp_redirect(home_url() . $user_check);
       					exit;
       				}
       				elseif($role == 'care_home_admin'){
       					var_dump("Role is correct");
       					if($belongs != $logged_in_post_id or $belongs = NULL){
       						wp_redirect(home_url() . "/home/" . $home_name);
       						exit;
       					}
       				}
       			}
       		}
       	}
       }
       ```
   
 * I’ve left out the bit where the variables are established as it seemed a waste
   of time, if it’s required I can post it later.
 * Here’s how it’s supposed to flow:
 * If the view is a patient view (the array returns the post_type, which is the 
   view or home in this example):
 * If the logged in user has the role of patient:
 * If the end of the url (which would look like siteurl/patient/patient_username)
   does not match the logged in username:
 * Redirect user to their own record (so the url ending in their username).
 * **Up until here, logged in as a patient, this works perfectly.**
 * Elseif logged in user has the role of care_home_admin:
 * If the record on screen does not belong to the users home (this is complicated
   but basically the database record for each patient has a field showing which 
   home they belong to, this is numerical and would basically be two strings reading“
   429” != “430” or something like that. If the patient the user is trying to view
   belongs to them then it should be to same number in each string.
 * Redirect to their home page for their home.
 * This second bit doesn’t work even though it’s identical in structure. If I use
   it as the primary if it causes an infinite redirect loop on every page accept
   the patients belonging to the home (so it’s obviously close) but as shown above
   it doesn’t seem to even get called.
 * I don’t know where I’m going wrong, if this is a numerical string issue or what,
   so if someone could offer some thoughts it would be greatly appreciated.
 * Many thanks.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/if-elseif-logic-error/#post-3392023)
 * In this line
    `if($belongs != $logged_in_post_id or $belongs = NULL){` you are
   assigning NULL to $belongs with ‘=’ instead of comparing with ‘==’
 * Unfortunately, you dropped the closing backtick, so the forum’s parser may have
   mangled your code, so the problem may be elsewhere.
 * FWIW, I don’t like comparing NULL and similar values, the comparison does not
   always yield the results you might expect. I would use either is_null() or isset(),
   depending on the context of how the variable gets assigned values.
 *  Thread Starter [theslink2000](https://wordpress.org/support/users/theslink2000/)
 * (@theslink2000)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/if-elseif-logic-error/#post-3392025)
 * Hi bud,
 * Sorry about the mild confusion I meant to take out the second half of that line,
   the
 * `or $belongs = NULL`
 * bit was simply me experimenting as other pages (ie. not patients) won’t have 
   a belongs field.
 * I’ve reposted the code here with the close in place (I hope) any more ideas?
 *     ```
       if(in_array('patient', $types)){
       	if($role == 'patient'){
       		if($path != $user_check){
       			wp_redirect(home_url() . $user_check);
       			exit;
       		}
       		elseif($role == 'care_home_admin'){
       if($belongs != $logged_in_post_id){
       			wp_redirect(home_url() . "/home/" . $home_name);
       			exit;
       					}
       				}
       			}
       		}
       	}
       }
       ```
   
 *  Thread Starter [theslink2000](https://wordpress.org/support/users/theslink2000/)
 * (@theslink2000)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/if-elseif-logic-error/#post-3392047)
 * I just figured it out for myself, it was as simple as I thought it might be, 
   the elseif was incorrectly nested for the logic to work. I accidently put it 
   as a second condition to the third if, not the second.
 * Simply moving the closing } to the correct place resolved everything!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘if / elseif Logic Error’ is closed to new replies.

## Tags

 * [elseif](https://wordpress.org/support/topic-tag/elseif/)
 * [if](https://wordpress.org/support/topic-tag/if/)
 * [logic](https://wordpress.org/support/topic-tag/logic/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 2 participants
 * Last reply from: [theslink2000](https://wordpress.org/support/users/theslink2000/)
 * Last activity: [13 years, 4 months ago](https://wordpress.org/support/topic/if-elseif-logic-error/#post-3392047)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
