Current course:

PHP If else

An "If else" statement or an "if block" allows for conditional execution of pieces of code (it's ok if that doesn't make sense just yet).

An If block checks to see if the value of it's expression is TRUE - if it is, it will continue and run it's statement:
if(expression) {
      statement
}

Examples

$a = 1;
$b = 2;

Example 1:
if($a == $b) {
      echo "kek";
}
//this checks to see if $a is equal (==) to $b - if it is, it will run the code inside it's { and }

Example2:
if($a != $b) {
      echo "kek";
}
//this checks to see if $a is NOT equal (!=) to $b - if it's not equal, then the statement is TRUE, which will make it continue and run the code inside it's { and }

Example 3:
if($a > $b) {
      echo "Phat Lewtz!";
}
//this will not echo anything, because $a is NOT greater than $b (sorry, no lewtz for you).

Example 4:
if($b > $a) {
      echo "1337";
}
//this will echo "1337" (because you're so 1337)
//This statement checks if the value of $b is greater than the value of $a. Since it is greater, it will continue and run the code between the { and the }.

Example 5:
if($a > $b) {
      echo "FTL";
} else {
      echo "FTW";
}
//this will echo "FTW"
//it will first check if $a is greater than $b (which it isn't), so it will continue, and run whatever is in the "else" brackets

Example 6:
if($a > $b) {
      echo "FTL";
} else if($b > $a) {
      echo "FTW";
}
//this will echo "FTW"
//it does the same as example 5, but instead of automatically running the 2nd bit of code, it first verifies that $b IS greater than $a instead of assuming that it is like example 5

Example 7:
if($a > $b && $a < $b) {
      echo "FAIL";
}
//this won't echo anything
//it checks to see if $a is greater than $b AND (&&) if $a is less than $b. BOTH have to be true, or the statement as a whole is false.
//(ie, you could not say that 2 is greater than 1 and 1 is greater than 2 - that's a false statement)

Example 8:
if($a > $b || $a < $b) {
      echo "Pwnt";
}
//this will echo "Pwnt";
//it checks to see if $a is greater than $b OR (||) $a is less than b
//since at least one of those statements is true, it will continue and run the code in it's brackets

PRACTICAL APPLICATION:

$name = "Dave";
$time = date('H:i');
if($time < '12:00') {
      echo "Good morning, ".$name;
} else if($time < '18:00') {
      echo "Good afternoon, ".$name;
} else {
      echo "Good evening, ".$name;
}