Web Engineering
Lecture 21-22
MAJID MUMTAZ Department of Computer Science, CIIT Wah
1
PHP Arrays
• What is an Array?
– An array stores multiple value in one signle variable.
Or we can say that
– ”An array is a special variable, which can hold more
than one same types of value”. • In PHP there are three type of arrays
Index arrays/Numeric: Array with numeric index.
1. 2. Associative arrays: Arrays with named keys 3. Multidimensional arrays: Arrays containing one or
more arrays
2
PHP Arrays
•
Index arrays/Numeric: A numeric array stores elements with a numeric ID key. The numeric index can be assigned automaticaly, and index will start at 0.
• Declaration:
$books = array(”php”, ”java”, ”c++”); OR $books[0] = ”php”; $books[1] = ”java”; $books[2] = ”c++”;
Example:
3
PHP Arrays
• •
The foreach loop statement: Is very handy to loop through arrays Syntax foreach(array as value) {
Code to be executed;
} Example
$books = array(”php”, ”java”, ”c++”); foreach($books as $value) {
echo $value;
echo ”
”;
}
?>
4
PHP Arrays
• Associative arrays: When storing data about specific
named values, a numeric array is not always the best way to do it. The alternate solution is associative array. With such arrays, each element is referenced by a string index. Typically a numeric index is called an index and a string index is called a key
• Associative arrays are arrays that use named keys that you assign to them when storing data about specific named values e.g. $price = array(”php”=> ”532”, ”java”=> ”340”); OR $price[’php’] = ”532”; $price[’java’] = ”340”;
5
PHP Arrays
• Using foreach statement in associative array Example: $price = array(”php”=> ”532”, ”java”=> ”340”); Foreach ($price as $key=>$value) {
echo $key. ”price is” . $value;
echo ”
”;
}
6
PHP Arrays
• Multidimensional Arrays: In MD array, 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
• Example: $shop = array(”laptop” => array(”Del”, ”Hp”, ”IBM”), ”printer” => array(”Hp”, ”Canon”)); echo $shop[’laptop’][2]; //IBM
7
PHP Functions • PHP Functions: Group of statement under single name
is called functions.
• A function is a block of code that can be executed
whenever we need it – Why functions are useful • To avoid duplicate code • To make code easier to eliminate errors
• Type of functions
– User-defined functions: Functions which are created by user or programmer is called user defined functions.
– Built-in functions: Built-in functions are also called ready-
made functions
• The real power of PHP comes from its functions • In PHP there are more than 700 built-in functions available
8
PHP Functions
Insert the function code
• All function start with the word “function()” • Name the function • Add a ”{”, the function code starts after the opening curly brace • • Add a ”}”, the function is finished by a closing curly brace. At the
end you must call a function by using their name. E.g.
Php
function test()
{
echo ”I am in test function”;
echo ”
”;
} test(); // Calling a function
?>
9
PHP Functions • Parameters Functions: A parameter is a variable that
holds the value passed to it when the function is called
• If we want to pass argument/parameter to the function, we place them b/w the parentheses, separating each argument by commas, i.e. – functionName (argument) – functionName(argument1, argument2, ...)
Example: Function test($a){
echo ”The value is” . $a;
} Test (12);
10
PHP Functions • PHP built-in functions: In PHP there are more than 700
built-in functions available – Some of the built-in functions are
• Sqrt() • Count() • Sort() and so on
• Example:
of 9 is 3
?>
11
PHP Functions
• Sorting of Arrays: The element in an array can be sorted in alphabetical or numeric order, i.e. Ascending or Descending
A - Z
Z - A
Small to large No. (1 to 10)
Large to Small No. (10 to 1)
12
PHP Functions
• Sort Function for Arrays
Sort ()
Sorts an indexed array in ascending order
Rsort()
Sorts an indexed array in descending order
Asort()
Sorts an associative array in ascending order, according to the value
Ksort()
Sorts an associative array in ascending order, according to the key
Arsort()
Sorts an associative array in descending order, according to the value
Krsort()
Sorts an associative array in descending order, according to the key
13
Examples
It will sort the element of the $books array in ascending alphabetical order
• Php $books = array(”php”, ”java”, ”Asp”);
sort ($books);
?> Sort the elements of the $numbers array in ascending numerical order Php $numbers = array(4,60,8,26,11); Sort($numbers); For ($a =0; $a<=$element;$a++)
{
echo $numbers[$a];
echo ”
”;
}
?>
14
Examples There is a function available in PHP i.e count() that can be
used to give you total number of array elements.
Php $books = array(”php”, ”java”, ”Asp”); $element = count($books); for ($a =0; $a<=$element;$a++)
{
echo $numbers[$a];
echo ”
”;
}
?>
15
PHP Include() and require() functions
• Include() function takes all the test in a
specified file and copies it into the file that uses the include function. Syntax: include(”header.php”); It is useful for modular programming. Another
function that is similar to include is the require() function: syntax is require (”head.php”);
16
Difference between include() and require() function
• The two functions are identical in every way,
except how they handle errors – The include() function generates a warning ( but
the script will continue execution)
– Require() function generates a fatal error ( and the
script execution will stop after the error) Note: Recommended function is require().
17
PHP Date() function
• Date() function is used to format a time or a date. The PHP date() function formats a timestamp to a more readable date and time.
date(format, timestamp);
Syntax: The first parameter in the date() function specifies how to format the date/time. It usese letters to represent date and time format. Some of the letter that can be used: – D – the day of the month (0 1– 31) – M – the current month, as a number (01 – 12) – Y – the current year in four digits. – 1 - Represents the day of the week
18
Note: Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting.
PHP Date() function
• What is a timestamp: Time stamp is the number of second since January 1, 1970 at 00:00:00 GMT. This is also known as the unix timestamp. Or • A timestamp is an integer that holds the number
of seconds from january 1, 1970.
• The mktime() function returns the unix
timestamp for a specified date.
Syntax: mktime(hour, minute, second, month, day,
year)
19
PHP forms & user input
• The php $_GET & $_POST variable are used to
retrieve information from forms, like user input. • PHP $_GET: The $_GET variable is used to collect
values from a form with method=”GET”.
• The $_GET variable is used to collect values from a form with a method=”GET”. Information sent from a form with the GET method is visible to everyone (It will be displayed in the browser’s address bar) and it has limits on the amount of information to send (max. 100 characters)
20
PHP $_POST Method
• The $_POST variable is used to collect values
from a form with method=”POST”
• The $_POST variable is used to collect values
from a form with method=”POST”. Information sent from a form with the POST method is invisible to others and has no limit on the amount of information to send.
21
Form Validation
• User input should be validated whenever possible. • Client side validation is faster and will reduce server
load, however any site that gets traffic to worry about server resources may also need to worry about site security.
• Best is to use server side validation if the form accesses
a database. A good way to validate a form on the server is to post the form itself, instead of jumping to a different page. The user will then get the error message on the same page as the form. This makes it easier to discover the error.
22
$_REQUEST with PHP
• $_REQUEST variable: variable can be used to get the result from data sent with both the GET and POST methods. i.e.

