5BUIS005C: Bis Design and Development
5BUIS005C: Bis Design and Development
Week 2
PHP
Continue with PHP
2
Arrays
■ An array is a special variable that holds more than one value at
a time.
■ For example we want to keep all the module taught at level 5
together.
■ We create an array that holds the names of all level 5 modules
■ Each element is actually a pair: a value and a key (used to
refer to the value).
■ The keys are a numbered index starting at zero but they can
also be named for easier access.
3
Arrays
How to define an array?
We use the built-in function array() and we assign it to a variable
4
Arrays
Numeric Arrays
■ A numeric array stores each array element with a numeric index.
■ 1. In the following example the index are automatically assigned (the index starts at 0):
$modules=array(‘BIS design’, ‘PHP’, ‘Analytics’);
5
Arrays
Associative Arrays
■ In associative arrays, each ID key is associated with a value.
array ($key=>$value)
Example 1
■ In this example we use an array to assign modules to different levels:
$modulesperyear = array(‘level 4’=>’Introduction’, ‘level 5’=> ‘BIS
design’, ‘level 6’=>’Project’);
6
Arrays
Example 2
8
Arrays
Using data from an array
We access data in an array by using:
the variable associated with this array (eg $modules),
followed by the key of the item we want to access enclosed into
square brackets (eg 0).
print_r($array_name);
$arrlength = count($array_name);
//use the key ($x; starting from 0) to refer to each member of the array
■ Associative arrays:
foreach ($array_name as $key=>$value)
{
…
echo ”key is ” . $key . ", value is" . $value; }
12
LOOPS
13
Loops
Create loops in PHP:
while (condition_is_true)
{
script
}
15
Loops
do
{
script
}
while (condition_is_true);
16
Loops
for (starting condition; terminating condition; change)
{
script;
}
17
Loops
foreach ($array_name as $key=>$value)
{
script;
}
Let’s see an example.
Create a loop to write on the screen all the numbers from 1 to 10 using:
while
do…while
for
foreach
Attention: for the last two (for; foreach) you must create arrays first.
18
Functions
19
Functions
A function is a piece of PHP code that can be executed once or many
times by the PHP script.
Structure of a function:
22
Functions & arrays – example
Let’s make our function more useful and use it with an array.
We create an array with all our guest and then, we call the
function from within a ‘for’ statement.
23
Functions, arrays, if statement
Let’s try something more advanced.
I have several friends from different countries. I want to wish the
each one ‘good morning’ in his/her own language.
What do I need?
Associative array with friends and countries
Function that generates ‘good morning’ message in different
languages taking into consideration the country (if statement).
Call the function from a ‘foreach’ statement that generates a
message for each of my friends.
24
Functions
There are many ‘built-in functions’.
For example the
array()
we used to create an array, is a ‘build-in’ function that creates an
array.
26
Next week
Having completed the introduction to PHP, we are now ready to start with:
Object Oriented PHP
Next week we’ll start OO PPH and we’ll discuss the following topics:
Classes and objects
$this
Encapsulation – identifiers – visibility
27