Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 37

2020

PHP Array
2020

Getting started with Array


2
What is an Array?
2020

• Arrays are special data types. Despite of other normal variables an array can
store more than one value. It contains set of data represented by a single
name.
• These are lists of information mapped with keys and stored under one
variable name

Syntax:
$array_name = array (values);

• Each piece of data stored in array is called element.


• An index is an element’s numeric position within the array.

3
Array in PHP can be written as
2020

$color = array(red, blue, green, yellow, black);

or

$color[] = “red”;
$color[] = “blue”;
$color[] = “green”;
$color[] = “yellow”;
$color[] = “black”;

4
Example
2020

$num = 7;
$foo = array(“this is an element”,5,$num);

echo $foo[0]; //prints : this is an element 


echo $foo[1]; //prints :5
echo $foo[2]; //prints :7

Index

5
with loop
2020

$days = array(“Sunday”, “Monday”, “Tuesday”,


“Wednesday”, “Thursday”, “Friday”,
“Saturday”);

for($i=0; $i<=6; $i++) {


echo “Day $i is $days[$i] <br/>”;
}

6
Associative Array
2020

These are arrays that uses name keys that


you assign to them.
Syntax:

$variable = array(‘name_key1’ => ‘value_1’,


‘name_key2’ => ‘value_2’,
‘name_key3’ => ‘value_3’);

7
2020

$scientist = array(‘Last Name’ => ‘Einstein’,


‘First Name’ => ‘Albert’,
‘Age’ => 129);

echo “Name: $scientist[‘Last Name’],


$scientist[‘First Name’]<br/>”;
echo “Age: $scientist[Age]”;

$scientist[‘Last Name’] = ‘Einstein’;


$scientist[‘First Name’] = ‘Albert’,
$scientist[‘Age’] = 129;

echo “Name: $scientist[‘Last Name’],


$scientist[‘First Name’]<br/>”;
echo “Age: $scientist[Age]”;
8
Lazy Function: foreach()
2020

foreach statement is used to iterate or loop


through the element in an array. With each
loop, a foreach statement moves to the
next element in an array.

foreach statement specify an array


expression within a set of parenthesis
following the foreach keyword.
9
Syntax of foreach()
2020

foreach(arrayVar as value)
{
   code to be executed;
}
foreach(arrayVar as key => value)
{
   code to be executed;
}
10
2020

<html>
<head><title> Using Array</title></head>
<?php
$scientist = array(‘Last Name’ => ‘Einstein’,
‘First Name’ => ‘Albert’,
‘Age’ => 129);
echo “<h3>Scientist Information</h3>”;
foreach($scientist as $inventor) {
echo “$inventor <br/>”;
}
?>
</html>

11
2020

$scientist = array(‘Last Name’ => ‘Einstein’,


‘First Name’ => ‘Albert’,
‘Age’ => 129);
echo “<h3>Scientist Information</h3>”;
foreach($scientist as $label => $inventor) {
echo “$label: $inventor <br/>”;
}

12
2020

$scientist = array(array(‘Einstein’, ‘Albert’, 129),


array(‘Da Vinci’, ‘Leonardo’, 130));
echo "<h3>Scientist Information</h3>";
for($a=0; $a<=2; $a++){
for($b=0; $b<=2; $b++){
echo $scientist[$a][$b]. “<br/>”;
}
}

13
2020

$scientist = array
(‘PHYSICS’ => array(‘lname’ => ‘Einstein’,
‘fname’ => ‘Albert’,
$field
‘age’ => 129), $inventor[
‘ANATOMY’ => array(‘lname’ => ‘Da Vinci’, ]
‘fname’ => ‘Leonardo’,
‘age’ => 130));

echo "<h3>Scientist Information</h3>";


echo "<table border=1 width=‘30%’><tr><th>Field</th>
<th>Last Name</th><th>First Name</th><th>Age</th><tr>";
foreach($scientist as $field => $inventor){
echo “<tr><td>$field”. “</td><td>”.
$inventor[‘lname’]. “</td><td>”.
$inventor[‘fname’]. “</td><td>”.
$inventor[‘age’]. “</td></tr>”; }
echo "</table>"

14
Sample Output:
2020

15
Sorting Arrays
2020

A common task you may find yourself doing with arrays is sorting
their values. PHP provides many functions that making sorting array
values easy. Here are just a few common array - sorting functions:
Array-Sorting Description
Function
sort($array) Sorts an array in ascending value order
rsort($array) Sorts an array in descending value order
asort($array) Sorts an array in ascending value order while
maintaining the key/value relationship
arsort($array) Sorts an array in descending value order while
maintaining the

16
Example
2020

$flavors = array(“Blue Raspberry”, “Root Beer”, “Pineapple”);


sort($flavors);
print_r($flavors);

$flavors = array(“Blue Raspberry”, “Root Beer”, “Pineapple”);


rsort($flavors);
print_r($flavors);

17
Example 2020

$flavors = array(“Fruit” => “Blue Raspberry”,


“Soda” => “Root Beer”,
“Chunks” => “Pineapple”);
asort($flavors);
print_r($flavors);

$flavors = array(“Fruit” => “Blue Raspberry”,


“Soda” => “Root Beer”,
“Chunks” => “Pineapple”);
arsort($flavors);
print_r($flavors);

18
2020

Getting started with Php functions on User-Defined Functions

19
User-Defined Functions
2020

Before you can use a function in PHP program, you must


first create, or define, it.
The lines that make up a function are called the function
definition.

Syntax:

<?php
function name(parameters) {
code to be executed;
}
?>
20
Example
2020

<?php
function writeName()
{
echo “Dr. Jose P. Rizal”;
}
echo "My name is ";
writeName();
?>

21
Parameters
2020

Parameters are placed within the


parentheses that follow the function name.

A parameter is a variable that is used within


a function. Unlike with other variables, to
declare a parameter, you only need to
place the parameter name within the
parentheses of a function definition.

22
2020

<?php
function writeName($fname)
{
echo $fname . " Rizal.<br />";
}

echo "My name is ";


writeName(“Jose”);
echo "My sister's name is ";
writeName(“Melchora”);
echo "My brother's name is ";
writeName(“Pepe”);
?>

23
Output:
2020

24
Returning Values
2020

A return statement is a statement that returns a


value to the statement that called the
function.
To let function return a value, use the return
<?php
function add($x,$y)
statement:
{
$total = $x + $y;
return $total;
}
// function which has the
parameter value of 1 and
echo "1 + 16 = " . add(1,16); 16
?> 25
Example
2020

<?php
function add($x=1, $y=16){
$total= $x + $y;
return $total;
}
echo “1 + 16 = " . add();
?>

26
Nested Functions
2020

<?php
function add($x=1, $y=16){
$total= $x + $y;
return $total;
}
echo “1 + 16 = " . add();
?>

27
Example
2020

function fname(){
echo "fname<br>";
function lname() {
echo "lname<br>";
}
}

output:
fname();
lname();

28
Understanding Variable Scope
2020

Variable scope can be either global


or local.

Global Variable is one that declared


outside a function and is available to all
parts of the program.
Local Variable is declared inside a
function and is only available within the
function in which it is declared. 29
Example
2020

<?php
$GlobVar = "Global Variable";
function scopeSamp() {
echo $GlobVar;
$LocalVar = "Local Variable
<br/>";
echo $LocalVar;
}
scopeSamp();
echo "$GlobVar <br/>";
echo "$LocalVar <br/>";
?>

30
The global Keyword
2020

In PHP, you must declare a global variable with the global


keyword inside of a function definition for the variable to be
available within the scope of that function.
<?php
$GlobVar = "Global Variable";
function scopeSamp() {
global $GlobVar;
echo $GlobVar; //prints successfully
}
scopeSamp();
?>

31
Example
2020

$num_of_calls = 0;
function numberedHeading($txt) {
global $num_of_calls;
$num_of_calls++;
echo "<h1>$num_of_calls. $txt</h1>";
}
numberedHeading("Widgets");
echo “<p> We build a fine range of widgets.</p>”;
numberedHeading("Doodads");
echo “<p>Finest in the world.</p>”;

32
Output
2020

33
The static Statement
2020

If you declare a variable within a function in conjunction with the


static statement, the variable remains local to the function, and the
function "remembers" the value of the variable from execution to
execution.

34
Example
2020

function Tables(){
static $count=0;
static $x=0;
$y=2;
$count++;
$x += $y;
echo "$x <br/>";
if($count < 5)
{
Tables();
}
}
Tables();
35
Ouput
2020

36
2020

Questions??

You might also like