Things You'll Need:
- Text editor or code programmer
- Hosting or Apache enabled server
-
Step 1
Rules
1. These tags must be contained within a PHP document and within PHP tags (< ? ? >).
2. An "if" tag can stand alone. However, if there is an "elseif" tag, there must be an "else".
3. An "else" tag cannot have limitations.
4. The format is as follows:
* if(condition){ //code here }
* elseif(condition){ //code here }
* else{ //code here } -
Step 2
Conditions are pretty easy to understand. Any of these tags are asking the question, "Is this true?". A condition is the "this" part of the question. A very simple example could be that the sun is bright. To put this into code, we write:
1 < ?
2 $sun = "bright";
3 if($sun === "bright"){
4 echo "The sun is bright today!";
5 }
6 else{
7 echo "The sun is not very bright today.";
8 }
9 ? > -
Step 3
Let's go through this line by line. Line 1 and line 9 begin and end the PHP code snippet.
Line 2 introduces a variable, "$sun". A variable can be identified by the "$" sign before it. Variables are traditionally composed of only alphabetic characters and underscores( _ ). Variables in PHP are traditionally lowercase, as variables are case sensitive.
Line 3 is our if statement. Translated into normal speak, it says, "If the variable "sun" matches the string, "bright", then...". The three = signs are used to designate that you want to match a string, exactly, to another one.
Line 4 is our conditional statement. If the if statement(line three) evaluates to true, then line four gets executed. Otherwise, it's like it never existed. In this case, we are outputting that our if statement was true. -
Step 4
Line 5 ends the if statement. We could just leave it at this, and not include the else statement. If $sun was not equal to "bright", then the page would display nothing.
Lines 6, 7 and 8 are our else statement. This is what happens if the "if statement" evaluates to false. Because there are only two options in this case, true or false, else does not get a conditional statement (in parentheses on line three).












