PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters
PHP Foundations

Basic Control Structures

04/05/2001

This article covers the basics of program control structures, including conditional statements and looping using the if and while statements in PHP. It also contains a brief note regarding embedding code blocks within other code blocks.

Conditional blocks

One of the most fundamental tools of any true programming language is the ability to control what code gets executed and under what conditions by using a conditional block. A conditional block can be thought of in English language terms as "if this, then that; otherwise something else". For instance: "If Billy has 5 dollars in his hand, then buy a candy bar; otherwise, cry." Today, we will discuss the basic structure and use of conditional blocks in our programs and introduce the ways we can use conditional blocks to compare one variable to another.

For this article, we'll be taking a look at the following PHP code:

<?php

 $dollars = 4;
  $have_candy = false;
 
  if($dollars == 5) {

   $have_candy = true;
   echo "Billy has a candy bar.<br />";

} else {

 echo "Billy could not afford any candy.<br />";
echo "Billy is crying.<br />";
}

echo "Billy went home.";

  if($have_candy) {

   echo "Billy ate his candy bar at home";
}
?>

The above is our first example of a conditional block and is the PHP version of our example mentioned in the introduction. When this code is executed, what will happen? Before we can properly answer this question, we first must learn some new syntax.

The if statement

The if statement is the most fundamental control structure available. Its function is to execute a "block" of code if and only if the statement provided to it is a "true" statement. In order to determine if a statement is indeed true, some comparison operators and their meaning must be introduced. Before that is covered, let's take a look at the if statement in general form:

If(conditions) {
  // Code if condition is true
 } [ else ] {
  // Code if condition is false
 }

Note: The presence of the brackets [ ] around the else portion of this general form indicates that it is not necessary to be a valid conditional. Another, incomplete general form of the if statement would be:

If(conditions) {
  // Code if condition is true
}

Now that we have a better understanding of the general form of an if statement, we can go back and examine our example. The parentheses of the first if statement contain $dollars == 5. Although it appears as if we are attempting (incorrectly) to assign the value 5 to the variable $dollars, in reality we are providing an execution condition for the code contained with the if statement. This condition (which can be read as $dollars must equal the value 5) will determine what code within that if statement is executed. There are many different comparison operators that are the foundation for building our conditionals, and they are listed below:

Comparison operators in PHP
$a == $b $a is equal to $b
$a != $b $a is not equal to $b
$a < $b $a is less than $b
$a > $b $a is greater than $b
$a <= $b $a is less than or equal to $b
$a >= $b $a is greater than or equal to $b

Looking back at our earlier example, we are now able to determine how our PHP code will behave. First, we initialized the variable $dollars with a value of 4 and the variable $have_candy with a Boolean value of false. Then, we compared the value of $dollars to see if it was equal to the constant value of 5. Since the value of dollars (4) was not equal to 5, $have_candy remained false and the output to the web browser was:

Billy could not afford any candy.
Billy is crying.
Billy went home.

What if we changed our conditions for the if statement? For instance, if we changed our conditional from equal (==) to less than or equal (<=), $have_candy would then be set to true and our output would be:

Billy has a candy bar
Billy went home.
Billy ate his candy bar at home.

Notice the behavior of the second if statement, which outputs "Billy ate his candy bar at home." In the first example, the variable $have_candy was false and, since there was no else statement for the second conditional block, it was simply skipped altogether. Another interesting behavior of the second if statement is the lack of any indication of what the variable $have_candy is being compared to. In cases such as this, where a variable is provided as the only condition to an if statement, the value of the variable is used to determine the behavior of the conditional. Therefore, since $have_candy had a value of true (referring to the second example), the conditional is considered to be true and, as expected, in the second example the code was executed.

Pages: 1, 2

Next Pagearrow




Recommended for You

  1. Cover of PHP Security Collection
    PHP Security Collection
    Ebook: $5.95
  2. Cover of Regular Expressions Cookbook
    Regular Expressions Cookbook
    Print: $44.99
    Ebook: $31.99
  3. Cover of Programming PHP
    Programming PHP
    Print: $39.95
  4. Cover of PHP Anthology
    PHP Anthology
    Print: $39.95

Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2010, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com