It's no doubt the If, else, else if statements are the most widely used in php. They are relatively easy too.
If Statement
This checks if x is true do a, if x is false do b. Syntax for a if statement is as follows:
if (expression) { statement here }
In the () you put the expression like 1>2 and such. The statement is what it should execute based on the expression. Time for an example.
$a = "1"; if ($a == "1") { echo 'There is $a apple'; }
Now if the condition does not meet what is needed to execute the code in the curly brackets, the code is skipped and moves on in the file. Example...
a = "1"; if ($a == "2"){ echo 'we have $a apples'; }
The code is skipped in the curly brackets in the example above. If statements are a fundemental of PHP and is required for any advanced programming.
Else Statements
Think of this as the If statements older brother. This executes code if the condition in the if statement is not met. Please note that you musthave a if statement before an else. Example time...
a = "2"; if (a !="2"){ echo 'a does not equal 2'; } else { echo 'a does equal 2'; }
The code in the if statement curlys are skipped but the code in the else curlys are used. As you can see Else statements are realitvely simple.
Else If
Think of this guy as the parent of this happy family. Else Ifs are an extention of the else statement. It allows you to supply a condition unlike the else statement. Once again, you need a if statement before else if. Else If statements also follow another crucial rule. There can only be 1 else if statement in a file. Adding anymore will give you a nasty parse error. Example time...
$a = "1"; $b = "2"; if ($a == "2") { echo 'a does not equal 2!'; } else if ($b == "2") { echo 'b however equals 2'; }
Else Ifs can get wicked. Of course so can If statements. You can also have if statements inside if statements and such but I don't have time to explain that stuff. Hope you learned alot from this tut. :)