PHP Snippets: If Statements

PHPThis post is part of the PHP Snippets series where I will be covering the basics of developing in PHP.

One of the most basic statemnts you’ll use in PHP is the if statement which is used for conditional statements which perform different actions.

The most basic form is the basic if itself which executes when one condition is true:

$time = date( "H" );

if ( $time < 12 ){
	echo "Good morning.";
}

This can be extended to supply a value if the original condition fails using an if...else:

$time = date( "H" );

if ( $time < 12 ){
	echo "Good morning.";
else
	echo "Hello."
}

And can be expanded further to check if one of multiple conditions is true using an if...elseif...else:

$time = date( "H" );

if ( $time < 12 ){
	echo "Good morning.";
}elseif ( $time < 18 ){
	echo "Good afternoon."
}else{
	echo "Good evening.";
}