PHPBuilder - Loops & Decisions in PHP - The ABC's of PHP Part 8

RSS Twitter

Loops & Decisions in PHP - The ABC's of PHP Part 8

by: PHP Builder Staff
May 13, 2009

In any given computer language (PHP is no exception) there has to be a way to allow the running code to decide between doing 2 different things.
If there wasn't then software would not be able to adapt based on operating conditions, or it wouldn't be able to decide between two different users.
In most languages the most basic of these is the "if" statement, which as its name implies does something if a given condition is true.
Let's look at a quick example:

  $name = "peter";
  if($name == "peter")
    print "Hello peter how are you today?\n";
    print "Sorry I don't know you\n";

As you can see it's very simple. First we have a string variable that holds a name. Then we have the "if" statement which says 'if the variable called name holds a value that is equal to peter then perform the program lines in the first set of curly brackets, otherwise perform the commands in the second set of brackets'
The first question you might ask is why do we have a double == sign, good question. This is because in PHP everything in a decision has to result in either true or false, and since we use = to give a value to a variable, then = will always be true (trust me, computers work that way ;-) ) and thus we would not be able to get a false. There are some cases where you have to use === (3 = in succession) but for most of the time == will do just fine.
Other Checks?
We don't just have to check to see if two values are equal, we can also check to see if they are not equal, or greater than and less than, the following list shows the different operators 'if' understands:
  • == - Are variables equal ( $A == $B )
  • != - Are variables NOT equal ( $A != $B )
  • >= - Is variable 1 greater than or equal to variable 2 ( $A >= $B )
  • <= - Is variable 1 less than or equal to variable 2 ( $A <= $B )
  • > - Is variable 1 greater than variable 2 ( $A > $B )
  • < - Is variable 1 less than variable 2 ( $A < $B )
You can also group one or more decisions together by using normal brackets, separated by any of the Boolean operators as shown below:
  • && - AND (Both decisions must be true)
  • || - OR (Either of the decisions must be true)
These are typically used in the following way:

  $fname = "peter";
  $lname = "shaw";
  if( ($fname == "peter") && ($lname == "shaw") )
    print "greetings Shawty, you are allowed in…";
    print "be-gone stranger, you are banished from here…";

This is now saying that fname must be equal to "peter" AND lname must be equal to "shaw" before I allow the top code to run, if not then the bottom code will run.
The full range of operators can be found in the php manual here and as always I strongly encourage you to read and experiment with different combinations.

Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.




(Maximum characters: 1200). You have characters left.