Intech 2201
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.
3
Declare & Initialize an Array
▪ Creating array through initialization
▫ $arrName = array(‘val1’,’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.
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 associative 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)
28
Formatting options in date() function
Argument Description
29
Formatting options in date() function cont..
30
Time Function
▪ Syntax
▫ time()
31
Formatting options in time() function
Argument Description
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.
35
Function
▪ A function is a reusable block of statements that performs a
specific task.
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.
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 "footer.php",
that looks like this:
45
include() – example 2
▪ Assume we have a standard menu file
called "menu.php":
46
include() – example 3
▪ Assume we have a file called "vars.php", with some variables
defined:
47
require()
▪ 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.
https://www.w3schools.com/php/default.asp
▪ w3resource (2020, February 26). PHP Tutorials for beginners.
https://www.w3resource.com/php/php-home.php
▪ Tutorials Point (2006). PHP Tutorial.
https://www.tutorialspoint.com/php/index.htm
▪ GeeksforGeeks (2021). PHP Tutorial.
https://www.geeksforgeeks.org/php-tutorials/
50
End
Any questions?
Email me at [email protected]
51