Allaire Cold Fusion Template Header Size: New VersionõO2¸~õìêÚ[�²zhqؤä8X°É¿Ìò©‰P^qv ßNÊÒ‡ùFÍû'ÉÊE´Žå¿ˆpc –¨u÷TéÒ•5˜¥ãxV’ôû”\v‰ù>eÐ×åRVŸ,ÛÚ÷î,™î(àñvЈN¯õáÜøç,Zê¤/„(|0}ã#ãJéî¥�UøÛ[Ô¢„+Ùyœ(·85ìhÿ—D¾!Èž›|S™ï¸]J € U[]†VŸ,ÛÚ÷– Controlling how your code flows Where your adventure begins
Showcase your website at Fusion GalleriesNetObjects Fusion 7 Websites Portal



On This Page

General Formatting

IF Statements

SWITCH Statements

WHILE Statements

FOR Statements

Controlling how your code flows

One of the great aspects of being evaluated at the server is that you can stick "snippets" of code into your NOF page anywhere, and as long as the {},(), <? ?> all match up, the HTML generated will depend on the flow control (or where you stuck those if statements or whiles.

In This Tutorial

PHP Tutorial Home

The Essentials

PHP in Fusion

Reading Files


Code Flow Control

Since PHP is open source, and loosely structured, there may be more than 1 way to write a control statement. For example, these two are evaluated identically, but look different



General Formatting

if ( $x==1)
     //then go off and do some stuff
   //stick around here and do some stuff

if ( $x==1):
     //then go off and do some stuff
   //stick around here and do some stuff

It does not matter which method you choose, and you can even mix and use both within a single PHP page, but you have to keep up with which } goes with what { and which endif; goes with what if:.

Since the bracket method is more widely used, I will use it in these examples. However in the nft sample files, you may see both.

There are two pages to cover this. The first is a bare-bones simple how to. The second shows you some tricks you can to using PHP to manipulate the objects on a Fusion page.

IF statements

IF statements perform a series of commands if the condition is met. They are fairly straight forward and easy to understand

  • statement conditions are enclosed in (  )
  • If statements can be nested

if (condition here)
   {   put PHP statements here; }


if (condition here)
  { put PHP statements here;}
  { put more PHP statements here;}


if (condition here)
   { put PHP statements here;  }
elseif (
another condition here )
   { put more PHP statements here; }
elseif (
yet another condition here )
  { put more PHP statements here; }
          //none of the other conditions have been met
  { put all the other PHP statements here; }

There is no limit to number of IF statements and IF statement's can be nested within other IF statements or within other types of conditional statements as well.

if ( $today<>"Monday"  )  
     $line = "The day is not Monday, it is $today";
 else  //today is Monday
 { if ( $timeofday < $noontime)  //var timeofday is less than var noontime
   { $line = "It is Monday morning"; }
   { $line = "It is Monday afternoon; }
echo $line;

SWITCH or "CASE" statements

Sometime you want to check and possibly execute more than one condition

Similar to a if/elseif except that if statements only affect 1 statement. Switch statements will evaluate each "case" until a 'break" is encountered. If there is no break, then PHP will execute all the statements and following the first "case" that was true. when a break is reached, the next line of code after then "switch" condition is executed.

switch(condition variable to check)
{ case "true":
   do some stuff here;
"another condition is true":
  do some more processing here;
    // no above conditions are true
   perform some default processing here...;

SWITCH Example

{ case "pizza":
   echo "I ate a pizza, I feel like a pig";
case "salad":
   echo "I ate a salad, I feel like a rabbit";
case "sunflower seeds":
   echo "I ate seeds, I feel like a chicken";
case "banana":
   echo "I ate a banana, I feel like a monkey";
default:             // no above conditions are true
   echo "I ate nothing, I feel like  camel";

WHILE statements

checks for a condition, then as long as that condition is true first, the statements within the brackets (called a "code block") will execute.

WHILE (condition here)
{ do some processing here

WHILE Example

$pizzaslices = 12;
$thereispizzaleft = $pizzaslices - $numbereaten;
while ($thereispizzaleft)

   $numbereaten = ($KWeb*2) + $numeaten;
   echo "Kris has eaten $numeaten slices of pizza<br>";
   $thereispizzaleft = $pizzaslices - $numbereaten;

FOR statements

performs repetitive tasks similar to a while statement, but you provide the beginning condition, ending condition (condition to "stop" to processing) and the increment-er at the start of the condition. This is an especially nice method of working through an a loop with a counter.

FOR (first condition, test condition,    increment condition   )
do some processing here

FOR Example:

$gfpiggies = array("turtle", "KWeb", "David", "Anton", "Tami", "Webber",
                     "Karl", "Werner", "Petra", "Charlie");

FOR ($i=1,   $gfpiggies[$i]<>"", $1+=1    )
{  echo ($gfpiggies[$i]." ate $i slices of pizza<br>");

There are also other types of looping and conditional statement that allow you to alter the flow of your PHP and HTML code.  These given are the most prevalent and most used.

Many more tutorials on PHP, and using it to access the MySQL database are available in the Members Section of the website.


View the PREVIOUS page in this tutorial

Return to the TOP of this page

View the NEXT page in this tutorial

|  Fusion  |  Web Design  |  Hosting  |  Resources  |  gotFusion Store  | 

Problems with this page?  

All content copyright © 2002 gotFusion LLC.  The name gotFusion and the gotFusion ® logo are registered trademarks of gotFusion LLC
Copyright, legal notice & privacy statement