Positive, then Negative (or the other way around)?

When you have an IF-THEN-ELSE, do you start with the “positive” portion first, and then save the “negative” for last, or visa-versa?

For example, Positive first…


	if (isset($_GET['x']) && strlen($_GET['x'])==40){
		// Valid Format.
		$tempPassword = $_GET['x'];
	}else{
		// Invalid Format.
		$_SESSION['resultsCode'] = 'PASSWORD_INVALID_TEMP_PASSWORD';

		// Redirect to Outcome Page.
		header("Location: " . BASE_URL . "members/change_password_results.php");

		// End script.
		exit();
	}//End of CHECK FOR TEMP PASSWORD

For example, Negative first…


	if (!isset($_GET['x']) && strlen($_GET['x'])!==40){
		// Invalid Format.
		$_SESSION['resultsCode'] = 'PASSWORD_INVALID_TEMP_PASSWORD';

		// Redirect to Outcome Page.
		header("Location: " . BASE_URL . "members/change_password_results.php");

		// End script.
		exit();
	}else{
		// Valid Format.
		$tempPassword = $_GET['x'];
	}//End of CHECK FOR TEMP PASSWORD

Debbie

Maybe off topic, but still:
this

 if (!isset($_GET['x']) && strlen($_GET['x'])!==40){ 

is not the negation of

if (isset($_GET['x']) && strlen($_GET['x'])==40){ 

This is the negation:

 if (!isset($_GET['x']) || strlen($_GET['x'])!==40){ 

So, even though IMO it makes no difference if you put the positive first or not, it might be easier, especially for if’s with composite conditions.

Basically, when performance matters, you want the IF statement to be the one that’s most frequent, but in PHP - performance of that kind of level is irrelevant, therefore you go with the one that is easiest to understand, which for most people is not negation (it would of been easier to understand if I said “which for most people is the positive first” - hopefully that illustrates my point).

So to answer your question, I would say the first is the better version, assuming your second one was fixed to be a correct negation.