| Navigation
|
|
|
| User Tools
|
|
|
| Site Statistics
|
|
|
Currently 0 registered users and 6 guests browsing.
29 articles available for viewing.
1313401 total page hits.
Welcome to our newest registered user, choonchan!
|
|
|
PHP/MySQL -> Tutorials
PHP Syntax (Control Structures): if, else and elseif
- by surreal on 05/26/06, 5:06PM CEST (3543 Views)
This article is a continuation from Basic Syntax of PHP.
The if-else statement
In this article I'm going to show you the syntax of 3 of PHP's most important control structures: the if, else and elseif statements. These 3 control structures operate under a very basic form of logic. The basic logic of the if-else is: if (this is true) { do this } else { do that }
Below is an example that better outlines this logic:
Code:
if ($user == "surreal") {
// User is surreal
print"You rock!";
} else {
// User is not surreal
print"You do not rock :(";
} |
|
As you can see, the if part of the if-else statement will only return true if the conditions inside the parenthesis are true. If they're not true, the script will simply move onto the next bit of code, in this case the else statement where we tell PHP what to do if the above if clause has not been returned true.
Another important note is the double equals sign (==). A double equals sign basically asks if the two objects in comparison are identically equal to eachother. A single equals sign will work just like basic PHP syntax and set the two objects equal to eachother, and if they're able to be set as equals, the if will return true.
If-else statements will work with any range of PHP syntax, including calculations and executing other functions; the logic will always remain the same.
Introducing elseif
If you want to add a separate set of conditions to your if-else statement, this can be done using elseif. The way this works: if the if statement is not true, the elseif offers another set of conditions that can be checked before moving onto the else statement.
Expanding the previous example:
Code:
if ($user == "surreal") {
// User is surreal
print"You rock!";
} elseif ($user == "surreal's mother") {
// User is surreal's mother (oh my goodness!)
print"You kinda rock!";
} else {
// User is not surreal or surreal's mother
print"You do not rock :(";
} |
|
That's pretty much all elseif's do.
Setting multiple conditions
You may wish to have more than one condition in your if statement. This can be done it two ways: using "and" or "or". This is expanding slightly on the logic presented previously, where we're now able to make our conditions more specified. Both of these expansions can be shortened to: "and" as "&&", "or" as "||".
Here is an example:
Code:
if ($user == "surreal" && md5($password) == $row["password"]) {
// User is either surreal with a valid password
print"You rock!";
} elseif ($user == "Coolio" || $user == "Zoinky") {
// User is either Coolio or Zoinky
print"You guys are ok";
} else {
// User isn't recognised
print"You suck!";
} |
|
Ands and ors can be combined in many different formats, so my advice is to just experiment as much as you can until you can fully understand what if-else and elseifs can do.
Standalone ifs
It's also important to note that an if does not require an else. If you simply want to check whether a condition is true without wanting anything to happen if it's not true, you can just use the if on its own:
Code:
if ($adminaccess > 0) {
print"You are an admin, here are your links:";
} |
|
The importance of inequalities
When using mathematics in your script, the if statement makes it very easy to use inequalities to compare two values. As shown in the example above, we can check if the value of $adminaccess is greater than zero. This is a very easy form of comparison.
The syntax for inequalities:
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Identically equal
!= Does not equal
And that's basically all there is to ifs, elses and elseifs!
In my next tutorial I will be teaching how to use for, while and do-while loops.
Seen this article somewhere else? Report it for plagiarism!
Noticed an error in this article? Tell us what's wrong!
View Comments for this Article
Comments
Comment by choonchan on 08/11/10, 10:44PM CEST
HDau0z [a href="http://jhrqopfnofgi.com/"]jhrqopfnofgi[/a], [url=http://idpyrtpyjgbh.com/]idpyrtpyjgbh[/url], [link=http://fhrzqtefhqmt.com/]fhrzqtefhqmt[/link], http://mhpoerapcapl.com/
|
| Comment by lymanknap on 08/04/10, 11:30PM CEST
uwMkl8 [a href="http://kukdyzegqdxd.com/"]kukdyzegqdxd[/a], [url=http://ytzracljivje.com/]ytzracljivje[/url], [link=http://usicefwzefeo.com/]usicefwzefeo[/link], http://ydxwffqcaqew.com/
|
|
Want to add a comment? Register a free account or Login if you already have one.
|
|
All content on this site is property of www.juxtaposing.com. Do not copy anything on this site without prior written permission of the site owner.
|
|
| © Copyright 2006 www.juxtaposing.com
|
|