0% found this document useful (0 votes)
10 views51 pages

PHP Arrays and String Functions Guide

The document provides an overview of PHP arrays, including their declaration, initialization, and types such as numeric, associative, and multidimensional arrays. It also covers string manipulation functions, date and time functions, user-defined functions, and the use of include and require statements in PHP. Additionally, it includes examples and syntax for each topic discussed.

Uploaded by

Taylor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views51 pages

PHP Arrays and String Functions Guide

The document provides an overview of PHP arrays, including their declaration, initialization, and types such as numeric, associative, and multidimensional arrays. It also covers string manipulation functions, date and time functions, user-defined functions, and the use of include and require statements in PHP. Additionally, it includes examples and syntax for each topic discussed.

Uploaded by

Taylor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INTECH 2201

LECTURE
1
Arrays
PHP Arrays

2
PHP Arrays
▪ An array is a data structure that stores
one or more similar type of values in a
single variable.

▪ For an instance, if you want to store 100


numbers, then instead of defining 100
variables its easy to define an array of
100 length.
3
Declare & Initialize an Array
▪ Creating array through initialization
▫ $arrName = array(‘val1’,’val2’,...);

▪ Creating array through individual


elements
▫ $arrName[0] = val1;
▫ $arrName[1] = val2;
▫ ...
4
Accessing Arrays
▪ for loop
▫ for($i=0;i<sizeof($arrName);$i++)
echo $arrName[$i];

▪ foreach loop
▫ foreach($arrName as $temp)
echo $temp;

5
Types of Arrays
▪ There are 3 different kind of arrays and
each array value is accessed using and ID
which is called array index.
▫ Numeric/Indexed Arrays
▫ Associative Arrays
▫ Multidimensional Arrays

6
Numeric/Indexed Array
▪ An array with a numeric index. Values are
stored and accessed in linear fashion.

▪ These arrays can store numbers, Strings


and any object but their index will be
represented by numbers. By default array
index starts from zero.

7
Numeric/Indexed Array
▪ There are two ways to create
numeric/indexed arrays.
▪ The index can be assigned automatically
(index always starts at 0)

▪ Or the index can be assigned manually:

8
Numeric/Indexed Array
▪ Sample Code
Result

9
Associative Array
▪ An array with Strings as index. This
stores element values in association with
key values rather than in a strict linear
index order.

▪ Associative array will have their index


as String so that you can establish a
strong association between key and
values.
10
Associative Array
▪ There are two ways to create associative
arrays.

▪ Or

11
Associative Array
▪ Sample Code

Result

12
Multi-dimensional Array
▪ An array containing one or more arrays
and values are accessed using multiple
indices.

▪ 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. Values
in the multi-dimensional array are
accessed using multiple index.
13
Multi-dimensional Array
▪ There are two ways to create multi-
dimensional arrays.

▪ Or

14
Multi-dimensional Array
▪ Sample Code

▪ Result

15
Multi-dimensional Array
▪ Sample Code
Result

16
Accessing Multidimensional Array
▪ Using nested loop

▪ Using foreach

17
2
String Manipulation
String Functions

18
String Functions
▪ PHP provides various String functions to
access and manipulate Strings
▪ PHP String functions are part of the PHP
core. No installation is required to use
these functions.
▪ Some String functions:
▫ strrev() trim()
▫ strcmp() parse_str()
▫ strlen()
19
strrev()
▪ The strrev() function is predefined
function of PHP.
▪ It is used to reverse a string. It is one
of the most basic string operations which
are used by programmers and developers.
▪ Syntax: strrev(String);
Result

20
strcmp()
▪ Is a string comparison function in PHP.
▪ It is a built-in function of PHP, which
is case sensitive, means it treats
capital and the small case separately.
▪ It is used to compare two strings from
each other.
▪ Syntax: strcmp(String1, String2);
Result

21
strlen()
▪ The strlen() function is predefined
function of PHP.
▪ It is used to find the length of string
or returns the length of a string
including all the whitespaces and special
character.
▪ Syntax: strlen(string);
Result

22
trim()
▪ The trim() function is predefined
function of PHP.
▪ It is used to strip whitespace from the
beginning and end of a string or both
side of a string.
▪ Syntax: strrev(String, charlist);
Result

23
parse_str()
▪ The parse_str() function is predefined
function of PHP.
▪ It is used to parse the string into
variables.
▪ Syntax: parse_str(String, array);

Result

24
String Functions
▪ These are only some of the useful String
manipulation functions that you can use.

25
3
Date & Time Manipulation
Date & Time Functions

26
Date & Time Functions
▪ PHP date function is an in-built function
that simplify working with date data
types.
▪ The PHP date function is used to format a
date or time into a human readable
format.
▪ It can be used to display the date of
article was published, record the last
updated a data in a database.
27
Date Function
▪ Syntax
▫ date(format, timestamp)

▪ A timestamp is a sequence of characters,


denoting the date and/or time at which a
certain event occurred.

28
Formatting options in date() function

Argument Description

d Represents day of the month; two digits with leading zeros


(01 or 31).
D Represents day of the week in the text as an abbreviation
(Mon to Sun).
m Represents month in numbers with leading zeros (01 or 12).

M Represents month in text, abbreviated (Jan to Dec).

y Represents year in two digits (08 or 14).

Y Represents year in four digits (2008 or 2014).

29
Formatting options in date() function
cont..

30
Time Function
▪ Syntax
▫ time()

▪ The time() function is used to get the


current time as a Unix timestamp (the
number of seconds since the beginning of
the Unix epoch: January 1, 1970, 00:00:00
GMT).

31
Formatting options in time() function
Argument Description

h Represents hour in 12-hour format with leading zeros (01 to


12).
H Represents hour in 24-hour format with leading zeros (00 to
23).
i Represents minutes with leading zeros (00 to 59).

s Represents seconds with leading zeros (00 to 59).

a Represents lowercase antemeridian and post meridian (am


or pm).
A Represents uppercase antemeridian and post meridian (AM
or PM).

32
Formatting options in time() function
cont..

33
4
User-defined functions
Functions

34
Function
▪ PHP has a large number of built-in
functions such as mathematical, string,
date, array functions etc.

▪ It is also possible to define a function


as per specific requirement. Such
function is called user-defined function.

35
Function
▪ A function is a reusable block of
statements that performs a specific task.

▪ This block is defined with function


keyword and is given a name that starts
with an alphabet or underscore.

▪ This function may be called from anywhere


within the program any number of times. 36
Function
▪ Syntax

Sample

37
User-defined function example
▪ No parameter/argument

Result

38
User-defined function example
▪ With parameter/argument
parameter

Result

} argument

39
User-defined function example
▪ With parameters/arguments

Result

40
5
PHP Include & Require
Files

41
PHP include and require files
▪ The include() and require() statement
allow you to include the code contained
in a PHP file within another PHP file.

▪ Including a file produces the same result


as copying the script from the file
specified and pasted into the location
where it is called.
42
PHP include and require files
▪ The include and require statements are
identical, except upon failure:
▫ require will produce a fatal error
(E_COMPILE_ERROR) and stop the script
▫ include will only produce a warning
(E_WARNING) and the script will
continue

43
PHP include and require files
▪ Syntax

44
include()
▪ Assume we have a standard footer file
called "[Link]", that looks like
this:

▪ To include the footer file in a page, use


the include statement:

45
include() – example 2
▪ Assume we have a standard
menu file called
"[Link]":

▪ All pages in the Web site


should use this menu file.
Here is how it can be done
(we are using a <div>
element so that the menu
easily can be styled with
CSS later):
46
include() – example 3
▪ Assume we have a file called "[Link]",
with some variables defined:

▪ Then, if we include the "[Link]" file,


the variables can be used in the calling
file:

47
include
▪ The require statement is also used to
include a file into the PHP code.
▪ However, there is one big difference
between include and require; when a file
is included with the include statement
and PHP cannot find it, the script will
continue to execute:

48
require()
▪ If we do the same example using
the require statement, the echo statement
will not be executed because the script
execution dies after
the require statement returned a fatal
error:

49
References
▪ W3Schools (2022). PHP Tutorial.
[Link]
▪ w3resource (2020, February 26). PHP Tutorials for
beginners. [Link]
[Link]
▪ Tutorials Point (2006). PHP Tutorial.
[Link]
▪ GeeksforGeeks (2021). PHP Tutorial.
[Link]

50
End
Any questions?
Email me at
[Link]@[Link]

51

You might also like