Alternatives to the If Else Statement in PHP

Alternatives to the If Else Statement in PHP

When I started programming on the first fundamentals I learned was the If Else Statement. The structure for the code for this is as following:

if(Conditional Statement) 
{
   // Do Something Here
} 
else 
{
  // Do Something Different Here
}

The Scenario

We are validating a Username with a function and we return a message. Below is how we would check to see if there was a Username using an If Else Statement.

function validateForm($username) 
{
    if($username != NULL) 
    {
        $message = "The Username is $username.";
    } 
    else 
    {
        $message = "There is no Username.";
    }
    return $message;
}

The Ternary Operator Statement

This perfectly fine way to check to see if there is a Username. But there is 7 lines of code inside the function and we could reduce it to 1 line of code. We would reduce it to one line by using a Ternary Operator Statement. This is one of my favourite comparison statements. Here is the structure to the comparison statement.

 return (Conditional Statement) ? true : false; 

So using the Ternary Operator statement we would just return a messsage for if there is or is not a Username submitted.

function validateForm($username) 
{
    return ( trim($username) != NULL ) ? "The Username is $username.": "There is no Username.";
}

So what happens if you need to check the username’s length is greater then 2 characters. This is how you would check the length with an If Else Statement.

function validateForm($username) 
{
    if($username != NULL) 
    {
	if(strlen($username) < 3) 
        {
	    $message = 'The Username needs to be at least 3 characters in length.';
	} 
        else 
        {
	    $message = "The Username is $username.";
	}
    } 
    else 
    { 	
        $message = 'There is no Username.';
    }
    return $message;
}

This is how we you an advanced Ternary Operator Statement to achieve the same result using 1 line of code. You can have as many Ternary Statement within another Ternary Statement. This is how we would achieve the above in 1 line of code.

function validateForm($username) 
{
    return ( trim($username) != NULL) 
    ? ( strlen($username) < 3 ) ? "The Username needs to be at least 3 characters in length." : "The Username is $username." 
    : "There is no Username.";
}

Now lets say we need to check the length of the Username is not greater then 10 and the Username cannot be Admin or Colin as they are in use by other User’s. Here is the if else statement to achieve this.

function validateForm($username)
{
    if( $username != NULL ) 
    {
        if( strlen($username) < 3 ) 
        {
            $message = "The Username needs to be at least 3 characters in length.";
	} 
        else if( strlen($username) > 10 ) 
        {
	    $message = "The Username needs to be at less than 10 characters in length.";
	} 
        else if( $username ==  'Admin') 
        {
	    $message = "The Username Admin is already in use.";
	} 
        else if( $username ==  'Colin') 
        {
	    $message = "The Username Colin is already in use.";
	} 
        else 
        {
	    $message = "The Username is $username.";
	}
    } 
    else 
    { 	
        $message = "There is no Username.";
    }
    return $message;
}

As you can see our if else statement is getting very long. No we can also reduce this a little bit and also make Colin and Admincase sensitive.

function validateForm($username){
    if($username != NULL) 
    {
        if( strlen($username) < 3 ) 
        {
	    $message = "The Username needs to be at least 3 characters in length.";
	} 
        else if( strlen($username) > 10) 
        {
	    $message = "The Username needs to be at less than 10 characters in length.";
	} 
        else if( strtolower($username) ==  'admin' || strtolower($username) == 'colin') 
        {
	    $message = "The Username $username is already in use.";
	} 
        else 
        {
            $message = "The Username is $username.";
        }
    } 
    else 
    { 	
        $message = "There is no Username.";
    }
    return $message;
}

The Switch Statement

Now we could use multiple ternary statements within each other but I personally find this hard to read and maintain. So what I would use instead is a Switch Statement.

This statement works using multiple case comparison and you can optionally have a default if none of the cases matched the comparison.Below is the structure of a switch statement.

switch($value){
    case 1:
         // Do something here if case 1 is matched
        break;

    case 2: 
         // Do something else here if this case 2 is matched
        break;

    default:
         // Do something here is non of the above comparisons were matched
        break;
}

So in this scenario our default will be the success message as the username did not match any of error validation comparisons.

function validateForm($username){
	$otherUsernames = array('admin', 'colin');
	switch( trim($username) ) {

	case NULL:
	    return "There is no Username.";
	    break;
	
        case strlen($username) < 3:
	    return "The Username needs to be at least 3 characters in length.";
            break;
	
	case strlen($username) > 10:
            return "The Username needs to be at less than 10 characters in length.";
            break;

	case in_array(strtolower($username), $otherUsernames):
	    return "The Username $username is already in use.";
	    break;

	default:
            return "The Username is $username.";
	    break;
	}
}

Conclusion

These are some different PHP alternative methods instead of the If Else statement. The purpose of the above examples is to reduce code, and make it easier to read and adjust when necessary. If you have any other methods please feel free to share below or if you have any questions about the code or functions above please leave a comment below.

Tags:
css.php