IPT home IPT A Virtual Approach by Peter Whitehouse
Quick Links:
 
 
Information and Intelligent Systems Social and Ethical Implications Human Computer Interaction Software and Systems Engineering eXercise Files Course Outline and Assessment A-Z of Geeky Acronyms Terrace Work Program 2004 Sillybus FAQ = Frequently Asked Questions Help
 
 

PHP Coding

Variables and Operators

Home | Variables & Operators | Input & Output | Decisions & Loops| Sub-Programming | Built-ins

PHP commands end with a SEMI-COLON, leaving them out often gives weird errors and will prevent your code running.

Programs typically use variables and PHP provides a simplified way of using them. Unlike other languages, PHP is said to be loosely typed - this means that the data types (integer, string etc.) variables can be do not need to be specified up front - the language can work them out based on your use of them. This is very liberating for a programmer. Additionally you are free to introduce variables when you need them, allowing coding to be a little more organic and free-flowing.

Commentation

PHP programs are typically rich with comments - remembering that you as a programmer are writing code that OTHERS will read and maintain, commentation is very important. Comments serve to annotate sections of the coded solution, explain methods used and point to possible issues when debugging. As a student you are required to use comments - get used to adding them as you go (rather than buy into the lie that you will add them later).

You will discover that your teacher often uses comments to map out the ALGORITHM first, before writing any commands.

Comments in PHP are relatively simple:

<?php

//this is used for a single line comment

echo 'Hello World';

/*
Multi-line comments go between these markers
useful for writing explanations
*/

?>

The // comment marker comments out anything to the right of the slashes, up until an end of line. For larger blocks of commented code, /* */ begin end pairs are used.

Comments have a VITAL role in debugging your code. You can switch off lines of program code by preceding them with a //, alternatively you can deactivate whole chunks of a program by using /* */ - useful in isolating parts of your programs that are not behaving themselves as you expect.

 

Variables and Type

To specify a variable, start with a dollar($) sign, for example:

<?php

$radius = 4.5;
$circum = 2 * pi() * $radius;
echo '<p>The Circumference of a circle with a radius of ' . $radius . ' is ' . $circum. '</p>';

?>

Variable names always start with a dollar sign, but must have a letter or extended ASCII code next, they cannot start with a digit or contain spaces or other weird punctuation. Good variable names will help others read your code (yes that IS important).

PHP variables are CASE SENSITIVE, so $a and $A are DIFFERENT variables! Typing carefully and sticking to some convention when naming variables is suggested. typically your teacher will camelcase starting with a lower case letter: $aLongPointlessName .

 

Structured Data Types

PHP is rich with structured data types - given the language was initially conceived as the interface between webpages and databases, there are a plethora of ARRAY operators and a myriad of ways to define and access them.

<?php
     $dwarves = array('sleepy','itchy','grumpy','sleazy');
     echo $dwarves[2];
     echo count($dwarves);
?>

Defines a 1 dimensional array with 4 littoral strings, indexed 0..3. The echo command, therefore, will output "grumpy". The count() builtin can be used to return the number of elements in the array

There are LOTS of types of arrays and LOTS you can do with them in your scripts - more here later.

 

Operators

PHP has a rich collection of operators used in a variety of ways. Basic mathematical operations like addition (+), subtraction (-), multiplication (*) and division (/) are fairly obvious, but it gets really interesting when delving deeper.

To assign a value to a variable, you use a SINGLE equals sign (=). This symbol means "takes on the value of", think of it as creating a box, labelling it with the name and storing the value inside. You can refer to the box by it's name, you can replace the value stored in the box by re-assigning it.

The statement:

<?php

$radius = 4.5;
$radius = 2* $radius;

?>

...creates a variable (a named memory location) called $radius, deduces it MUST be a real number after type juggling, allocates enough bytes of memory for that type of data and then stores the value 4.5 at that location. Almost immediately afterwards, the program fetches the current value for $radius, multiplies it by 2 and then stores the answer back in the $radius variable, overwriting the previous value - this is re-assignment.

Increment/Decrement operators exist and like most C language variants there is a PRE and POST increment that changes WHEN the value is incremented.

<?php

$i = 5;
echo $i;
echo $i++;  //POST increment - output the current value of i and then increase it by 1
echo ++$i;  //PRE increment - increase the current value of i by one and then output it

?>

++$i is a PRE-INCREMENT - it increments the variable "i" BEFORE using it, $i++ POST increments it (ie. it uses the variable value as is and then increments it after using it. Both types of increment have their uses, be careful however as you should be in control of when the change occurs.

The += construct lets us increment by a supplied amount ( $x +=24 for example increases the numeric variable $x by 24). Clearly -=, *= and /= are also possible, using subraction, multiplication and division incrementors respectively.

Real division is achieved with a /, integer division is trickier - thereis no single operator but one approach is to use the floor($x/$y) builtin to deliver the integer component of the division.

% returns the MODULUS (the remainder AFTER division) and ** raises to the power.

 

wonko@wonko.info
©Copyright t 1992..2018+. Edition 26.150117
wonkosite
Creative Commons License
This work is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 2.1 Australia License
.