Basic Control Structures in PHP
By Darren W. Hedlund2005-06-20
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.
<?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.
Tutorial pages:
|
|
|||||||||
You might also want to check these out:
|
Link to This Tutorial Page!

