Theory 
NSW Schools
Maths
Science
Documentation
Efficiency
Media
Databases
E-commerce
Reporting
Reviews
Data
Simulation
Maths
Computing
Encoding
 

Theory

Page title

PHP - Variables & Control Structures

Variables

Control Structures - if and while loops

Control-structures, such as if statments and While Loops, control the flow or execution through your program. Without control structures programs would consist of a sequence of linear steps and programs (scripts) would always execute in these same series of steps.

Assigning a Value to a Variable & Stringing Variables Together

$firstname="Barney"

The source code for the file variables1.php which displays the variable $firstname as output is:

<html><HEAD></HEAD><body>
<?php  $firstname="Barney";  ?>
<?php echo "$firstname"; ?>
</body></html>

You string variables together using the concatination operator as follows:

$firstname="Barney" $surname="Rubble"

The source code for the file variables1a.php which displays the variable $firstname followed by $surname as output is:

<html><HEAD></HEAD><body>
<?php  $firstname="Barney";   $surname="Rubble"; ?><BR><?php echo "$firstname" . " $surname"; ?>
</body></html>

You can pass the variables to be put together using the get or post URL method.

The source code for the file variables1b.php which displays the variable $firstname followed by $surname as output is:

<html><HEAD></HEAD><body>
<?php echo "$firstname" . " $surname"; ?>
</body></html>

Try changing the values of $firstname and $surname in the URL which access this script. For example:


Integer Variables

<?php $age = 25; echo $age; ?>

The source code for the file variables2.php which displays the variable $age as output is:

<html><HEAD></HEAD><body>
<?php  $age = 25; echo $age;  ?>
</body></html>

Calculations with Integer Variables

The source code for the file variables3.php which adds together a number of variables and produces their sum as output is:

<html><HEAD></HEAD><body>
<?php
  $a = 4;
  $b = 7;
  $c = 2 + 4 * $a + 5 * $b
  /* This is evaluated like this 2+ (4 * 4) + (5 * 7) = 53 */;
  echo $c;
  ?>
</body></html>

The sum is evaluated like this:
2+ (4 * 4) + (5 * 7) = 53

Some similar examples were sourced from http://www.gimpster.com/php


Inserting Comments


IF Statments and While Loops

IF - ELSE statements are used to force programs to take different paths depending on the evaluation of a given condition. The general form of an IF statement is:

<?php
if(expression evaluates to TRUE)
{
evalute the code or dispatch the contents inside these curly brackets
};
else
{
evalute the code or dispatch the contents inside these curly brackets
}
?>

Here is the code to execute a sample if statment

<html><head></head><body>
<?php
if($name=="Barney")
{
echo "Hello Barney Rubble You are my favourite cartoon character";
}
else
{
echo "Where is Barney Rubble?";
}
?>
</body></html>

You can execute this script using the following URLs:

WHILE loops are used to iteratively process a section of program until such time as a condition is violated. The general form of a WHILE loop is:

<?php
while(condition evaluates to TRUE)
{
iteratively evalute the code inside these curly brackets
};
?>

The following code block provides an example of a while loop
<html><head></head><body>
<?php 
$total=0; 
while($total<=20) 
{ 
echo("$total" . ",&nbsp;"); 
$total=$total+2; 
};
echo("<br>Condition violated, maximum value exceeded, loop terminated");
echo("<br>Code beneath loop will now be executed"); 
?>
</body></html>

You can execute this script using the URL:

Inline Image - Go To www.precisioninfo.com