WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1]
SELECT rating_username, rating_rating, rating_ip FROM wp_ratings WHERE rating_postid =

PHP Tutorial #3 – IF Statements

Vote This Post DownVote This Post Up (+1 rating, 1 votes)
Loading ... Loading ...

PHP Tutorial #3 – IF Statements

In order to make a successful PHP website, it is crucial to know how to correctly use if statements. IF statements, similar to CASE, have many advantages. Before we go further into these handy tools, let’s examine what they can do.

The IF statement allows you to control different parts of your code based on input. An example when this would be used is if you have a menu that limits access based on different permissions. Look at the menu below:

Home
Links
Your Profile
Post News
Register an Account

With this particular menu, you would want everyone to have access to the “Home” and “Links” options. However, if a user comes on they do not have an account you would want them to see the “Register an Account” option. The “Your Profile” and “Post News” options would not be available. Now let’s say that person creates an account. You would want them to be able to access the “Your Profile” option, but you would not want them to “Post News” or register another account. As a last scenario, let’s say you log in. You would want to have access to the first four options. However, since you already have an account, there is no need for you to see the last option.

The use of IF statements allows us to control these options. There are several forms of IF that we can use.

IF – If there is a certain situation, execute this code.
ELSE – If the IF statement returns FALSE, execute this code.
ELSE IF – If the IF statement returns FALSE but you want to have additional options, continue with this.

The ELSE IF is a tricky to understand so I will go over that more in a second. Now let’s view the syntax. To make this simple, I will start with a basic IF statement.

In this situation I will check to see if a number is greater than another number.

$1 = 1;

if($1 > 5) {

echo “$1 is greater than 5″;

}

else {

echo “$1 is less than 5″;

}

The above code does several things. First, I define $1 as “1″. I then use the syntax

if(VALUES) {

CODE TO EXECUTE;

}

Note that I enclose the IF values in between parentheses. In this specific instance, I will not use a semicolon after the brackets. This is one of the only times a semicolon will not be used. The compiler checks and sees that 1 not greater than 5. Since the IF statement returns FALSE, the compiler will execute the code in ELSE.
Note: An else statement is not required. If you only have an IF statement and that statement returns false, the compiler will continue on with other parts of the code.

Since the IF statement is FALSE, the user would view:

1 is less than 5

Note that there are many options with IF statements. Follow the below syntax for all of your IF statements.

Variable versus another variable or number.

if($variable > $/#) – Checks to see if a variable is greater than a variable/number.
if($variable < $/#) – Checks to see if a variable is less than a variable/number.
if($variable >= $/#) – Checks to see if the var is greater/equal to a var/number.
if($variable <= $/#) – Checks to see if the var is less than/equal to a var/number.
if($variable == $/#) – Checks to see if the var is equal to a variable/number.

Variable versus string

Use the same syntax as above, however you need to use quote marks around the string.

Example:

if($variable == “String”), if($variable < “String”)

IF statements can also include two parameters. To link two parameters we have two options.

If you want to have two possibilities (if x = x OR x = y)…

if(($variable1 == $variable2) || ($variable1 < $variable3))

The above checks to see if $variable1 is equal to v2. If that isn’t the case, it will also check to see if $variable1 is less than v3. You can do this with AND.

If you want to combine two IF statements into one…

if(($variable1 == $variable2) && ($variable1 < $variable3))

Now you can make IF statements as complex as you want. Make sure you you use proper syntax. With the below statement I will have two cases.

if((($variable 1 == $variable2) && ($variable3 < $variable4)) || ($cat == $dog))

The above statement is fairly complex. First it checks to see if var1 is equal to var2. If that statement is true, it checks to see if var3 is less that var4. If both of those statements are true, it will return the entire IF statement as TRUE. Now, if var1 is not equal to var2 or var3 is not less than var 4 it will continue with
the statement. If the the first part is FALSE it will check to see if cat is equal to dog. If that portion of the statement is true, it will return the entire IF statement as true. Note, var1 could be equal to var2 OR var3 could be less than var4. Unless the first two parts are equal, that portion of the IF statement will be
FALSE. This can be hard to understand, so review the statement several times until you fully understand it. I now want to add on one more option. In addition to the == (equal to) you can also use != (not equal to). != can be used wherever == can be used and visa versa.

Now that we have IF statements down, I will tell you that you can use more than one IF statement inside another. This basically does the same thing as above, but in this case you are expanding it out. I will show you the above IF statement divided into two parts.

if($variable1 == $variable2) {

if($variable3 < $variable4) {

}

}

else if($cat == $dog) {

}

The above does several things. First, it checks to see if var1 is equal to var2. If that’s true, it checks to see if var3 is less than var4. As mentioned above, both portions need to be true in order for that part of the statement is true. It doesn’t matter if var3 is less than var4 if var1 is not equal to var2. In this case we did
not want to use just ELSE. If we used ELSE no matter the outcome of the first part, we would surely execute our statement. I used ELSE IF to connect the two together. I could have technically made $cat == $dog it’s own IF statement.

if($variable1 == $variable2) {

if($variable3 < $variable4) {

}

}

if($cat == $dog) {

}

The above does the exact thing as the two previous examples. One time when ELSE IF comes in handy is when you are checking to see if one particular variable equals something.

if($home == “1″) {

}

else if($home == “2″) {

}

else if($home == “3″) {

}

else {

}

The above allows us to divide up home and assign different outcomes based on the value of home. I will now go back and show you how we can execute our menu.

Home
Links
Your Profile
Post News
Register an Account

I will use the variable $account. Here are the options.

If the user does not have an account, $account will equal nothing (”"). If the user is logged in under a normal account, $account will be equal to “normal”. If the user is an administrator, $account will be equal to “admin”. Since account will not be equal to multiple things, I will use separate IF statements. Note that the below code would be incorrect in an actual php page. The option names would have to be replaced with actual code.

Home
Links
if(($account == “normal”) || ($account == “admin”)) {
Your Profile
}
if($account == “admin”) {
Post News
}
if($account == “”) {
Register an account
}

The reason I don’t use ELSE IF in this case is becase multiple things could be shown at once. ELSE IF would be used if there is only one outcome. With the above example, Home and Links are displayed no matter what. If the user is a regular member or an administrator, they will be able to view their profile. If the user is an administrator, they will be above to post news. If the user is not a member at all ($account == “”, or nothing), they will have the option to register a new account. It is very common to receive a parse error when working with IF statements. Make sure you use correct syntax, you close all of your brackets, and you don’t add semicolons after the brackets. There are many other cases when the IF statement could be used. You will see this material in later tutorials.

THIS tutorial was written by Christopher Airey. Chris works as the lead system administrator at ReadySetConnect Web Hosting. Looking for a good PHP web host for your scripts?

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Webnews
  • MisterWong
  • Y!GG

One Response to “PHP Tutorial #3 – IF Statements”

  1. For PHP,mysql,Ajax codings all can visit this tutorial blog
    http://mycodings.blogspot.com . In this blog there are interview questions for php,mysql programmers. It might be useful for the novice

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>