0% found this document useful (0 votes)
29 views

5BUIS005C: Bis Design and Development

This document provides an overview of arrays, loops, and functions in PHP. It introduces arrays as variables that can hold multiple values, including numeric and associative arrays. It discusses using for, while, do-while, and foreach loops in PHP. It also covers defining functions to reuse code, including passing arguments and returning values. Built-in functions and exploring PHP references are also mentioned. The document aims to provide foundational PHP concepts after an initial introduction to the language.

Uploaded by

Shakir Bari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

5BUIS005C: Bis Design and Development

This document provides an overview of arrays, loops, and functions in PHP. It introduces arrays as variables that can hold multiple values, including numeric and associative arrays. It discusses using for, while, do-while, and foreach loops in PHP. It also covers defining functions to reuse code, including passing arguments and returning values. Built-in functions and exploring PHP references are also mentioned. The document aims to provide foundational PHP concepts after an initial introduction to the language.

Uploaded by

Shakir Bari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

5BUIS005C

BIS DESIGN AND DEVELOPMENT

Week 2
PHP
Continue with PHP

This week we’ll cover:


■ Arrays
■ Loops
■ Functions

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

E.g. we want to create an array with modules taught at level 5:

$modules = array (‘BIS design’, ‘PHP’, ‘Analytics’)

variable built-in function Elements


array() (titles of level 5 modules)

4
Arrays
Numeric Arrays
■ A numeric array stores each array element with a numeric index.

There are two methods to create a numeric array.

■ 1. In the following example the index are automatically assigned (the index starts at 0):
$modules=array(‘BIS design’, ‘PHP’, ‘Analytics’);

■ 2. In the following example we assign the index manually:


$modules[0]= ‘BIS design’;
$modules[1]=‘PHP’;
$modules[2]= ‘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

■ This example is the same as example 1, but shows


a different way of creating the array:

$modulesperyear[‘level 4’] = ’Introduction’;


$modulesperyear[‘level 5’] = ‘BIS design’;
$modulesperyear[‘level 6'] = ‘Project’;
7
Arrays
Multidimensional Arrays
In a multidimensional array, each element in
the main array can also be an array.
And each element in the sub-array can be an
array, and so on.

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).

$modules=array(‘BIS design’, ‘PHP’, ‘Analytics’);


echo $modules[0]

$ages = array(‘Peter’=>32, ‘Mary’=>25, ‘Jane’=>24);


echo $ages[‘Mary’]
9
Arrays
Add a new value in an associative array:
array_push ($array_name, “value1”, “value2”…);

Generate ALL the values in an array:

print_r($array_name);

(let’s try this with the previous arrays we created)


10
Arrays
Other methods:
■ Numeric arrays:
//define the length of the array

$arrlength = count($array_name);
//use the key ($x; starting from 0) to refer to each member of the array

for($x = 0; $x < $arrlength; $x++)


{
echo $array_name[$x];
}
Let’s try it with the previous arithmetic array 11
Arrays

■ Associative arrays:
foreach ($array_name as $key=>$value)
{

echo ”key is ” . $key . ", value is" . $value; }

Let’s try it with the previous associative array.

12
LOOPS

■ As we saw in previous files, very often we


are in the need to create a loop.

■ There are several ways to create loops in


PHP.

13
Loops
Create loops in PHP:

■ while - loops through a block of code while a specified


condition is true
■ do...while - loops through a block of code once, and then
repeats the loop as long as a specified condition is true
■ for - loops through a block of code a specified number of times
[starting condition; terminating condition; change]
■ foreach - loops through a block of code for each element in an
array
14
Loops

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

We might need to execute the same piece of code several times

It is a waste of time / effort to write the code again and again

Alternatively, we could write the code once then give a name to


this piece of code and call it every time we need it 
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:

function function_name($arg_1, $arg_2)


{ body of function ; }

The word “function” at the beginning


The name of the function followed by arguments within parenthesis. If
there are no arguments then: function_name()
Followed by the body of function within curly brackets
20
Functions
I want to write a function that welcomes my visitors. Visitors are given
by variables. I do not want to repeat the “echo” statement.

The following function does this:


function welcome($variable)
{
echo “Welcome ” . $variable ;
}

Inside my program I just need to call the function: welcome ($var1);


Define function BEFORE the first time you call it
21
Functions
Let’s try it.
Use the code in the previous slide and create the function ‘welcome;.
Define a variable and instantiate it with a name (e.g. ‘Peter’).
Call the function using a variable.
Call the function using a constant.

Apparently every time we have a new guest, we need to call the


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.

Much less coding…

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.

There is a huge list of built-in functions


You can explore them:
https://www.w3schools.com/php/php_ref_array.asp
25
Summary
Last week and today we had an introduction to PHP.

We went through the basic syntax and the main procedures of


PHP.

The basic syntax and the main procedures apply to both


procedural and object oriented PHP.

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

You might also like