Breadcrumbs

PHP Conditional

In your PHP code you will want to make decisions doing one piece of code if something equals this and something else if it equals this. To do this we use what is called conditional statements. PHP supports two conditional statements, if...else statements and switch statements.

if...else Statements

The if statement is used if a condition is true to execute some code and if is false will execute nothing, or other code. Here let's say that we get user input from a previous page and we want to only execute some code if that information was entered within the form field.

Code: PHP

<?php
$input1 
$_POST['input1'];

if (!empty(
$input1)) {
    echo 
"Your input is $input1.";
}
?>

Using the empty() function from PHP along with the NOT operator to make the if statement read. If $input one is not empty execute the following code. We can also add an else statement on to the back of the if statement to make it an if...else statement:

Code: PHP

<?php
$input1 
$_POST['input1'];

if (!empty(
$input1)) {
    echo 
"Your input is $input1.";
}
else {
    echo 
"You need to fill in all required fields.";
}
?>

This is an example of simple form validation. In an if...else statement the else statement is not required and when you leave it out no action is taken if the conditional statement isn't true. Below the proper format for an if...else statement:

Code: PHP

<?php
if (conditional statement) {
    
}
else {
    
}
?>

Again the else statement is not a requirement. Within the curved braces ({ }) you place the code that you want to be executed if the conditional statement is true/false. To have a proper conditional statement it is also required that you have a knowledge of comparison and logic operators. A good tutorial on this is PHP Variables and Operators.

You can also use more complex comparison statements. Let's say we have two user input fields and want to check that both of them hold a value. Using logic operators along with comparison operators then we can make this one if...else statements.

Code: PHP

<?php
$input1 
$_POST['input1'];
$input2 $_POST['input2'];

if (!empty(
$input1) && !empty($input2)) {
    echo 
"Your input is $input1 and your second input is $input2.";
}
else {
    echo 
"You need to fill in all required fields.";
}
?>

This if...else statement uses the AND operator (&&) to connect two comparison statement so that both of them must be true for the code to execute.

Switch Statements

A switch statement is used when you have multiple possibilities for the if statement. For example if you want that if the value is 1 to do this, if it is 2 do this, and if neither do something else. The switch statement provides a much more organized way to do this than having nested if...else statement. A nested if...else looks like:

Code: PHP

<?php
if ($value == 1) {
    echo 
"Value equals 1";
}
else {
    if (
$value == 2) {
        echo 
"Value equals 2";
    }
    else {
        echo 
"Value is neither";
    }
}
?>

This nested if...else statement is difficult to read. Lets take a look at the proper format for a switch statement:

Code: PHP

<?php
switch (variable) {
    case 
option1:
        
code to be executed if expression label1;
    break;
    case 
option2:
        
code to be executed if expression label2;
    break;
    default:
        
code to be executed
        
if expression is different 
        from both label1 
and label2;
    break;
}
?>

The case option1 and case option2 are the values that the variable may be. default is if none of the case options are true. Now here is the nested if...else statement that we used above as a switch statement.

Code: PHP

<?php
switch ($value) {
    case 
1:
        echo 
"Value equals 1";
    break;  
    case 
2:
        echo 
"Value equals 2";
    break;
    default:
        echo 
"Value is neither";
    break;
}
?>

As you see this switch statement is much more organized than the nested if statement seen above. Using switch statements for multiple if...else statements is the best method around and easy to use.

Now with the knowledge of conditional statements you can make your programs make decision.