Tutorial 10: If Statements


The point of coding in PHP is to make your website dynamic—or smart—so that it can make logical decisions. What I mean by this is your website can make decisions on what to do next based on user input, user conditions, or parameters you've set yourself. Here are some examples:

Let's start with the If Statement. It works something like this:

If THIS then THAT

Okay, that was a little vague. Let's expand on that a bit:

If expression is TRUE, then do something

If expression is FALSE, then don't do anything

Back to If Statements

Let's see what they look like in PHP:

if (expression) {

	// code to execute if expression evaluates to TRUE
	
}

If we expand on that further, it looks more like this:

<?php

	// Set some variables
	$a = 20;
	$b = 50;
	
	if ($a < $b) {
		
		echo "Yep! $a is certainly less than $b.";
		
	}

?>

As you can see, we are using the "less than" symbol (<) to check if the variable $a is smaller than the variable $b. If the expression is TRUE, then PHP will echo the text "Yep! 20 is certainly less than 50."

If statements can be very powerful, but these are just some very basic examples to get you started.

Check out the final example
©2019 Brad Hussey