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

Week 5 - Supplementary Lectures - 6 (Arrays and Some String Functions)

This document discusses PHP arrays and the foreach loop. It covers how to create and access arrays in PHP using the array() construct or [] syntax. The document also discusses how to get the size of an array, add/remove elements, and iterate through arrays using the foreach loop. PHP arrays can be indexed or associative, and the foreach loop allows iterating through all elements of an array without knowing the size upfront.

Uploaded by

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

Week 5 - Supplementary Lectures - 6 (Arrays and Some String Functions)

This document discusses PHP arrays and the foreach loop. It covers how to create and access arrays in PHP using the array() construct or [] syntax. The document also discusses how to get the size of an array, add/remove elements, and iterate through arrays using the foreach loop. PHP arrays can be indexed or associative, and the foreach loop allows iterating through all elements of an array without knowing the size upfront.

Uploaded by

secret
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

APPDET

APPLICATION DEVELOPMENT &


EMERGING TECHNOLOGY

Prof. ROEL C. TRABALLO


[email protected]
INTRODUCTION TO WEB DEVELOPMENT
 PHP Arrays and ForEach
 PHP String Functions

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
 PHP ARRAYS
• By definition, an array is a list of elements. So, for
example, you may have an array that contains a
list of products.
• PHP provides you with two types of arrays:
indexed and associative.
• The keys of the indexed array are integers that
start at 0. Typically, you use indexed arrays when
you want to access the elements by their
positions.
• The keys of an associative array are strings.
• And you use associative arrays when you want to
access elements by string keys.

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
 PHP ARRAYS: Creating Arrays
• In PHP, you can use the array() construct or [] • To create an array with some initial
syntax to define an array. elements, you place a comma-
• The [] syntax is shorter and more convenient. separated list of elements within
parentheses of
1) Creating an array using array() construct the array() construct.
To define an array, you use the array() construct. • For example, the following defines
 The following example creates an empty an array that has three numbers:
array: <?php
<?php
$scores = array(1, 2, 3);
$empty_array = array();

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
 PHP ARRAYS: Creating Arrays
• In PHP, you can use the array() construct or [] • The following example uses
syntax to define an array. the [] syntax to create a new array
• The [] syntax is shorter and more convenient. that consists of three numbers:

2) Creating an array using the [] syntax <?php


PHP provides a more convenient way to define $scores = [1, 2, 3];
arrays with the shorter syntax [], known as JSON
notation. The following example uses [] syntax to
create a new empty array:
<?php

$empty_array = [];

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
 PHP ARRAYS: Displaying arrays
• To show the contents of an array, you use • Or you can use the print_r() function:
the var_dump() function. Example:
Example: <?php
<?php
$scores = array(1, 2, 3);
$scores = [1, 2, 3]; print_r($scores);
var_dump($scores);
Output: Array
Output: array(3) { (
[0]=> int(1) [0] => 1
[1]=> int(2) [1] => 2
[2]=> int(3) [2] => 3
} )

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
 PHP ARRAYS: Displaying arrays • It’s more convenient to define a
• To make the output more readable, you can
function that prints out an array
wrap the output of the print_r() function inside
like this:
a <pre> tag.
Example: Output: Example: <?php
<?php Array function print_array($data) {
( echo '<pre>';
$scores = [1, 2, 3]; [0] => 1 Output: print_r($data);
[1] => 2 echo '</pre>';
echo '<pre>'; Array
[2] => 3 }
print_r($scores); (
)
echo '</pre>'; [0] => 1
[1] => 2 $scores = [1, 2, 3];
[2] => 3 print_array($scores);
)

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
PHP ARRAYS: Accessing array elements
• To access an element in an array, you specify • Adding an element to the array
the index of the element within the square • Syntax:
brackets: $array_name[] = new_element;
• Syntax:
Example: • In this example, we
$array_name[index] defined an array that
<?php consists of three
• Note that the index of the first element of an numbers initially.
array begins with zero, not one. $scores = [1, 2, 3]; • Then, we added the
$scores[] = 4; number 4 to the array.
Example: <?php Output:
• But doing this, you have to calculate the new
1 index manually. It is not practical.
$scores = [1, 2, 3]; • Also, if the index is already is used,
echo $scores[0]; the value will be overwritten.

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
PHP ARRAYS: Changing array elements Removing array elements
• The following statement changes the • To remove an element from an array, you
element located at the index to use the unset() function.
the $new_element: • The following removes the second element
• Syntax: of the $scores array:
$array_name[index] = $new_element; Example:
<?php
• Note that the index of the first element of an
array begins with zero, not one. $scores = [1, 2, 3];
Example: <?php Output: unset($scores[1]);
1
$scores = [1, 2, 3];
echo $scores[0];

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
PHP ARRAYS: Getting the size of an array
• To get the number of elements in an array, SUMMARY
you use the count() function. For example: • Use the array() construct or [] syntax to
create a new array.
Example: Output: • For the indexed array, the first index begins
<?php 5 with zero.
• To access an array element, use an index in
$scores = [1, 2, 3, 4, 5]; the square bracket $array_name [index].
• Use the count() function to get the number
echo count($scores); of elements in an array.

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
PHP ARRAYS: foreach loop
• PHP provides you with the foreach statement that allows you to
iterate over elements of an array, either an indexed array or
an associative array.
• The foreach statement iterates over all elements in an array, one at a
time. It starts with the first element and ends with the last one.
• Therefore, you don’t need to know the number of elements in an
array upfront.
<?php
• Syntax:
foreach ($array_name as $element) {
// process element here
}

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
PHP ARRAYS: foreach loop
Example: • When PHP encounters the foreach statement, it
<?php
accesses the first element and assigns:
 The key of the element to the $key variable.
$colors = ['red', 'green', 'blue'];  The value of the element to
the $value variable.
foreach ($colors as $color) {  In each iteration, PHP assigns the key and
echo $color . '<br>';
}
value of the next element to the variables
($key and $value) that follows
Output: red the as keyword.
green  If the last element is reached,
blue PHP ends the loop.

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
PHP ARRAYS: foreach loop
Example: Output:
<?php The capital city of Japan is Tokyo
The capital city of France is Paris
$capitals = [ The capital city of Germany is Berlin
'Japan' => 'Tokyo', The capital city of United Kingdom is London
'France' => 'Paris', The capital city of United States is Washington D.C.
'Germany' => 'Berlin',
Summary:
'United Kingdom' => 'London',
• Use the foreach($array_name as $element) to
'United States' => 'Washington D.C.'];
iterate over elements of an indexed array.
• Use the foreach($array_name as $key => $value)
foreach ($capitals as $country => $capital) {
to iterate over elements of an associative array.
echo "The capital city of {$country} is $capital" .'<br>';
}

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
PHP ARRAYS: Associative Arrays • Adding elements to an associative array:
Associative arrays are arrays that allow you to <?php

Example:
keep track of elements by names rather than by
$html['title'] = 'PHP Associative Arrays';
numbers.
$html['description'] = 'Learn how to use
• Creating associative arrays: To create an associative arrays in PHP';
associative array, you use the array() construct:
<?php print_r($html);
Output:
$html = array(); Array
(
[title] => PHP Associative Arrays
[description] => Learn how to use associative arrays in PHP
)

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: ARRAYS
PHP ARRAYS: Associative Arrays
• Accessing elements in an associative array: Summary:
• To access an element in an associative array, • Use an associative array when you want
you use the key. to reference elements by names rather
Example: than numbers.
<?php

$html['title'] = 'PHP Associative Arrays';


$html['description'] = 'Learn how to use associative
arrays in PHP';

echo $html['title'];

Output: PHP Associative Arrays

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


INTRODUCTION TO WEB DEVELOPMENT
PHP FUNDAMENTALS: STRING FUNCTIONS
Some String Functions
SYNTAX DESCRIPTION EXAMPLE OUTPUT
strlen(string) returns the length of a string. strlen(“Hello World”); 11
converts the first character of a string
ucfirst(string) ucfirst(“hello world”); Hello world
to uppercase
converts the first character of each
ucwords(string) ucwords(“hello world”); Hello World
word in a string to uppercase
lcfirst(string) Convert the first character to lowercase lcfirst(Hello World”) hello World
strtoupper(string) converts a string to uppercase strtoupper (“hello world”); HELLO WORLD
strtolower(string) converts a string to lowercase strtolower(“HELLO WORLD”); hello world
strrev(string) reverses a string. Strrev(“hello world”) dlrow olleh

APPDET | APPLICATION DEV’T & EMERGING TECH. | Prof. ROEL C. TRABALLO


APPDET
APPLICATION DEVELOPMENT &
EMERGING TECHNOLOGY

APPDET | APPLICATION
COMPROG2 | COMPUTERDEV’T & EMERGING
PROGRAMMING TECH. | |Prof.
2 (Elective) Prof.ROEL
ROELC.C.TRABALLO
TRABALLO

You might also like