|
|
#1 (permalink) |
|
GotGames Moderator
|
Chaos' PHP Tutorial - Part 3: Conditional Statements.
Well, as promised here is the third installment of the epic series of PHP tutorials! This time we will be focusing on Conditional Statements. The previous tutorials contain some knowledge that is needed in this one, so it is advised that you read them (if you already haven't) before attempting to understand this one. This tutorial will most likely be more complicated for beginners than the last one, but bear with it and soon you'll understand! Conditional Statements Conditional statements are used to tell the script what to do when certain conditions are met. There are a few different types of conditional statements, I will list a few. Code:
* If statement * Else statement * Elseif statement * While statement First we'll start with the two most used and basic conditional statements; the if and else statement. The if statement is used in PHP just as it could be used in regular conversation: PHP Code:
PHP Code:
PHP Code:
PHP Code:
The curly brackets { and } are much like the delimiters of PHP; they tell the script to execute everything between them if the conditions that were given in the expression are true. We now know what happens when the conditions are true, what about if they are false? Normally if the conditions of the if statement are false, the statement to be executed will just be ignored. However, we can tell the script what to do in the case of it being false! The Else Statement The else statement is basically the alternative which is executed if the conditions of the if statement are wrong. Its syntax is as follows: PHP Code:
PHP Code:
In this case, it is the former of the two as $x is less than $y; therefore, the message that "$x was less than $y!" would be displayed. If we changed $x to 10 and $y to 5, the results would change. PHP Code:
This would result in the message of "$x was greater than $y!" being printed to the screen. That's all good and well, but what if we wanted to add another variable? PHP Code:
The Elseif Statement The Elseif statement basically acts as another if statement, allowing you to give two different conditions for the parser to evaluate. Its syntax is as follows: PHP Code:
PHP Code:
After that it says elseif x is less than or equal to z (in this case x and z are equal) then echo the message. Lastly it says else echo "I don't know!" The else is there for the possibility of y being greater than z and etc. The While Statement The while statement is the simplest form of a loop in PHP. Basically, it tells PHP to repeatedly execute the statement as long as the given expression remains true. The syntax for it is: PHP Code:
PHP Code:
It then tells PHP to increment (add) to the value of $i. After $i++ is run, $i would = 2. Since it is a loop, the script will be executed again and again until $i equals 5. It would also print to screen the numbers 1 to 5. That pretty much sums up this tutorial on conditional statements. I won't tell you what the next tutorial is about because it will be a nice suprise. Good luck, have fun and remember to PM me if you need help! --Chaos. |
|
|
|
|
|
|
|
#4 (permalink) |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
There is also the ternary Statement:
PHP Code:
This can be a handy nugget say: $var = isset($_GET['var']) ? parseInput($_GET['var']) : $varDefault; sets $var to GET['var'] iff it is set, or varDefault if not. (iff = if and only if btw)
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
|
|
|
|
|
#10 (permalink) |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
Following is C code.
PHP Code:
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
|
|
|
|
|
#11 (permalink) | |
|
GotGames Moderator
|
Quote:
Using index.php?page= which will then use the switch statement to include a certain file dependent on the expression they added after the =. But you know what I'm on about.
Last edited by chX; 04-05-2007 at 10:33 PM.. |
|
|
|
|






