Using IF Statements in PHP

Using IF Statements in PHP


The IF statement is a critical element in every programming language. It allows for branching based on specific conditions that must be met.

if (certain_condition == True) { action }

Here are some code examples demonstrating IF statements in PHP:

1. Even Numbers

Even Number IF Statement

2. Odd Numbers

Odd Number IF Statement

The code above will display “Even Number” or “Odd Number” depending on the value of the variable. For instance, if number = 30, it is an even number; if number = 31, it is an odd number. The comparison operator == is used to evaluate statements that result in either True or False.

When run in a browser, it looks like this:

  • Odd Number Result: Odd Number Result

  • Even Number Result: Even Number Result

The simple IF statement used above provides only one resolution; if the condition is not met, the action block is skipped. For scenarios with two possible outcomes, you can use the IF-ELSE structure:

if (certain_condition == True) {
    // Action if condition is True
} else {
    // Action if condition is False
}

That concludes our discussion on using IF statements in PHP. Stay tuned to the Kuasai Teknologi blog for more basic programming tutorials!

Thanks for reading, and don’t forget to practice immediately!