Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Basic Control Structures in PHP

By Darren W. Hedlund
2005-06-20


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.



Tutorial Pages:
» Conditional blocks
» The if statement
» Basic looping
» The while statement
» Infinite loops
» Notes on embedding code blocks


 | Bookmark
Related Tutorials:
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources