Web Technologies 2: Arrays & Functions
Web Technologies 2: Arrays & Functions
Mohammed 1
PHP arrays
• An array stores multiple values in one single
variable.
7
Indexed arrays
• Keeps track of these data items by using
sequential numbers.
8
Associative arrays
• A string value index is used to look up or
provide a cross-reference to the data
value.
• Example :
$stuff = array("php" => "moh",
"html" => "ahmed");
9
Associative arrays
• Adding an Associative Array Item:
– $stuff ["css"] = "ali";
10
Multidimensional arrays
• A multidimensional array is an array
containing one or more arrays.
11
Multidimensional arrays
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
); <?php
echo $cars[0][0].": In stock: ".$cars[0][1].",
sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].",
sold: ".$cars[1][2].".<br>";?> 12
Accessing array elements
• Array elements can be accessed using the
array[key] syntax.
– <?php
– $array = array(
– "foo" => "bar"
– );
– var_dump($array["foo"]);
– ?>
13
Looping Through an Array:For
• $grades = array(66, 72, 89);
$sum = 0;
for (i=0; i < count(grades); i++)
$sum += $grades[i];
average = $sum/count(grades);
14
Looping Through an Array :foreach
foreach($stuff as $k => $v ) {
//foreach ($stuff as $stf) for only value
echo "Key=",$k," Val=",$v,"\n";
}
?> Key=name Val=moh
Key=course Val=PHP 15
Array functions
• The count() Function:
– The count() function is used to return the
length (the number of elements) of an array:
– echo count($cars);
• array_search()
– Searches an array for a given value and
returns the key
16
Array functions
• count($ar) - How many elements in an array
• is_array($ar) - Returns TRUE if a variable is an array
• isset($ar['key']) - Returns TRUE if key is set in the array
• sort($ar) - Sorts the array values (loses key)
• ksort($ar) - Sorts the array by key
• asort($ar) - Sorts array by value, keeping key association
• shuffle($ar) - Shuffles the array into random order
• array_sum() - Sum numerical values in the array.
• array_pop( ) : Remove an item from the end of an array.
• array_push( ) : Add an item to the end of an array.
17
Example
$arr = array();
Outputs :
$arr["name"] = "moh";
Count: 2
$arr["course"] = "PHP";
$arr Is an array
print "Count: " . count($arr) . "\n";
name is set
if ( is_array($arr) ) {
addr is not set
echo '$arr Is an array' . "\n";
} else {
echo '$arr Is not an array' . "\n";
}
echo isset($arr['name']) ? "name is set\n" : "name is
not set\n";
echo isset($arr['addr']) ? "addr is set\n" : "addr is not
set\n"; 18
Arrays and Strings
• Equality $x == $y
– Returns true if $x and $y have the same key/value pairs
• Inequality $x != $y
– Returns true if $x is not equal to $y
20
Print_r
Outputs :
Array
<?php (
$x = array ('x' => 'Dept', [x] => Dept
'y' => 'Employee', [y] => Employee
'z' => array ('a', 'b', 'c')); [z] => Array
print_r ($x); (
?> [0] => a
[1] => b
[2] => c
)
) 21
FUNCTIONS
22
Functions
• The real power of PHP comes from its functions.
23
Functions
• PHP does not support function
overloading
24
PHP Built-in Functions
25
PHP Built-in Functions
• is_number( ) Function: Testing whether a
variable is a valid number or numeric string.
It returns true or false.
26
PHP User Defined Functions
• Functions are defined using the function
statement.
• Syntax :
function function_name(parameters, arguments)
{
command block
}
function printName($name) {
• echo (“<HR> Your Name is <B><I>”);
echo $name;}
printName("mohammed" ); 27
Functions
• Returning Results:
function cube($number) {
$result = $number * $number * $number;
return $result;
}
$y = cube(3);
28
Functions
• In PHP Function, arguments may be used
to transfer information to functions. A
variable is the same as an argument.
29
Passing Arguments By Reference
• Arguments are generally passed by value in
PHP, which ensures that the function uses a
copy of the value and the variable passed
into the function cannot be modified.
30
Passing Arguments By Reference
<?php
function add_some_extra(&$string)
{
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str; // outputs 'This is a string, and
something extra.'
?>
31
Optional Arguments & default
values
function OutputLine($text, $size=3, $color =
“black”)
{
echo “<font color = $color size = $size>
$text </font>
}
32
Functions within functions
<?php
function fun1()
{
function fun2()
{
echo "I don't exist until fun1() is called.\n";
}
}
/* We can't call fun2() yet since it doesn't exist. */
fun1();
/* Now we can call fun2(), fun1()'s processing has
made it accessible. */
33
Recursive functions
<?php
function recursion($a)
{
if ($a < 20) {
echo "$a\n";
recursion($a + 1);
}
}
?>
34
• For arrays
– https://www.php.net/manual/en/language.
types.array.php
• For functions
– https://www.php.net/manual/en/language.
functions.php
35
Any Questions?
36