Conditional Statements in PHP

Conditional statements in PHP utilize the keywords if, else, and else if. Conditions are placed inside parentheses immediately after the if and else if keywords. Here’s an example:

//example of an if statement:
if($x < 10) {
    printf("x is less than 10\n");
}

//example of an if-else statement:
if($x < 10) {
    printf("x is less than 10\n");
} else {
    printf("x is 10 or more \n");
}

//example of an if-else-if statement:
if($x < 10) {
    printf("x is less than 10\n");
} else if($x === 10) {
    printf("x is equal to ten\n");
} else {
    printf("x is greater than 10\n");
}

Observe that the statement, if($x < 10) does not have a semicolon at the end. This is because it is a conditional statement that determines the flow of control and not an executable statement. Therefore, no semicolon is used. Suppose we made a mistake and did include a semicolon:

$x = 15;
if($x < 10); {
printf("x is less than 10\n");
}

This PHP code will run without error or warning. However, it will end up printing x is less than 10, even though x = 15! Recall that a conditional statement binds to the executable statement or code block immediately following it. In this case, we’ve provided an empty executable statement ended by the semicolon. The code is essentially equivalent to

$x = 15;
if($x < 10) {
}
printf("x is less than 10\n");

This is obviously not what we wanted. The semicolon was bound to the empty executable statement and the code block containing the print statement immediately followed, but was not bound to the conditional statement which is why the print statement executed regardless of the value of x.

Another convention that we’ve used in our code is where we have placed the curly brackets. If a conditional statement is bound to only one statement, the curly brackets are not necessary. However, it is best practice to include them even if they are not necessary and we’ll follow this convention. Second, the opening curly bracket is on the same line as the conditional statement while the closing curly bracket is indented to the same level as the start of the conditional statement. Moreover, the code inside the code block is indented. If there were more statements in the block, they would have all been at the same indentation level.

PHP Conditional Examples

Let’s look at an example where we read input from users and then check if its valid or not. The first thing we need to do is establish the variables we’ll need and read them in from the user. At the same time we can check for bad input (negative values) for both the inputs.

//prompt for income from the user
printf("Please enter your Adjusted Gross Income: ");

$income = floatval(trim(fgets(STDIN)));

//prompt for children
printf("How many children do you have? ");
$numChildren = intval(trim(fgets(STDIN)));

if($income < 0 || $numChildren < 0) {
    printf("Invalid inputs");
exit(1);
}

// Next, we can code a series of if-else-if statements for the income range. 
// By placing the ranges in increasing order, we only need to check the 
// upper bounds.

if($income <= 18150) {
    $baseTax = $income * .10;
} else if($income <= 73800) {
    $baseTax = 1815 + ($income -18150) * .15;
} else if($income <= 148850) {
    // ...
} else {
    $baseTax = 127962.50 + ($income - 457600) * .396;
}

// Next we compute the child tax credit, taking care that it does not exceed
// $3,000. A conditional based on the number of children suffices as at this 
// point in the program we already know it is zero or greater.

if($numChildren <= 3) {
    $credit = $numChildren * 1000;
} else {
    $credit = 3000;
}

// Finally, we need to ensure that the credit does not exceed the total tax 
// liability (the credit is non-refundable, so if the credit is greater, the
// tax should only be zero, not negative)

if($baseTax - $credit >= 0) {
    $totalTax = $baseTax - $credit;
} else {
    $totalTax = 0;
}

Licenses and Attributions


Speak Your Mind

-->