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

Printer Version of Page

Theory

Page title

PHP - Variables & Control Structures

Variables

  • Variables are used to store information for later use. This provides efficiencies in processing information since it is necessary to update only one variable value and all calculations depending on it will be automatically revised.
  • Variables may be of a number of distinct kinds depending on the nature of the information they represent. Variables may represent Integers, floating point numbers (Doubles), pieces of text (Strings), true or false propositions (Booleans), or other kinds of things called Objects or Arrays. In PHP most of the time the processor figures out what type a variable it is dealing with from the content of the variable and the context in which the information is being processed.
  • Variables must start with the $ character. After that there must be a letter. The rest of the name can consist of both letters and numbers. Here are three examples of valid names for a variable: $name, $address2 and $colour_30. The _ (underscore) character is used to replace blank spaces since blank spaces are forbidden.
  • Types of variables: Numbers (not sure whether this means integers or the larger field of real numbers)

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

  • /* Comments may be inserted beteen slashes and stars like this (as shown in the previous example)*/
  • //everything after a double slash like this, but on the same line, is also treated as a comment

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