Jump to content

If statement checking


Xtremer360

Recommended Posts

Does this if statement look right to anyone?

 

$user_id should be a a number and the reg key is a string

 

if ((is_numeric($user_id)) && ((isset($registration_key)) && (!empty($registration_key) && (!is_null($registration_key))) && ($registration_key == ''))

Link to comment
https://forums.phpfreaks.com/topic/267712-if-statement-checking/
Share on other sites

its wrong. you first tested to see if (!empty($registration_key) which means that if its empty the condition will not be true but at the end you again test to see if it == to nothing which means the condition will be true if it == to nothing.

 

your statement is contradicting.

I would recommend you to change your code to "Exit Early". That means checking for error conditions, and if an error is found exit/return from the function.

That will help you cut down on the levels of nesting, and make your code a lot easier to read. As it is now, I have to scroll up and down just to see what conditions those error messages belong to.

You know, you could just have searched for "exit early programming", and found a whole lot of references on the subject. Amongst them, the following Wikipedia article:

http://en.wikipedia.org/wiki/Control_flow

 

Searching for "return" in the PHP manual would also have given you quite a few examples.

I've even posted a few posts here on PHPfreaks where I've explained how to do this, with code.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.