Lecture 05
By
Shahab Ul Islam
PHP Arrays
An array is a special variable, which can hold more
than one value at a time.
In PHP, the array() function is used to create an array:
Syntax:
array();
Array cont…
In PHP, there are three types of arrays:
Indexed arrays
Associative arrays
Multidimensional arrays
PHP Indexed arrays
Arrays with a numeric index.
There are two ways to create indexed arrays.
The index can be assigned automatically like this:
$cars = array(“Mercedes", "BMW", "Toyota");
Or
the index can be assigned manually:
$cars[0] = " Mercedes ";
$cars[1] = "BMW";
$cars[2] = "Toyota";
Example of Indexed Arrays
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2]
. ".";
?>
</body>
</html>
The count() Function
The count() function is used to return the length (the
number of elements) of an array:
Example:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
PHP Associative Arrays
Associative arrays are arrays that use named keys that
you assign to them.
There are two ways to create an associative array:
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
Or
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
Example of Associative Array
<!DOCTYPE html>
<html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
</body>
</html>
PHP - Sort Functions For Arrays
The elements in an array can be sorted in alphabetical or numerical
order, descending or ascending.
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the
value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the
value
krsort() - sort associative arrays in descending order, according to the
key
Example 01
<!DOCTYPE html>
<body>
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>"; }
?>
</body>
</html>
Example 02
<!DOCTYPE html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>"; }
?>
</body>
</html>
Multidimensional Arrays
In multidimensional array each element in main array
can also be an array.
And each element in sub-array can be an array and so
on.
Example:
<?php
$shop=array(
“laptop”=>array(“dell”, ”sony”, ”HP”),
“printer”=>array(“HP”, “Canon”));
?>
PHP Global Variables – Super globals
Several predefined variables in PHP are "super
globals", which means that they are always accessible,
regardless of scope - The PHP super global variables
are:
$GLOBALS
$_SERVER
$_POST
$_GET
PHP $GLOBALS
$GLOBALS is a PHP super global variable which is
used to access global variables from anywhere in the
PHP script (also from within functions or methods).
PHP stores all global variables in an array called
$GLOBALS[index]. The index holds the name of the
variable.
Example:
<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
PHP $_SERVER
$_SERVER is a PHP super global variable which holds
information about headers, paths, and script locations.
$_SERVER['PHP_SELF'] Returns the filename of the currently
executing script.
$_SERVER['SERVER_ADDR']Returns the IP address of the
host server.
$_SERVER['SERVER_NAME']Returns the name of the host
server.
$_SERVER['REQUEST_METHOD']Returns the request
method used to access the page (such as POST)
Example:
<?php
echo $_SERVER['SERVER_NAME'];
echo "<br>";
$_SERVER['GATEWAY_INTERFACE']
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
PHP $_POST
PHP $_POST is widely used to collect form data after
submitting an HTML form with method="post".
$_POST is also widely used to pass variables.
Example:
<?php
$name = $_POST['fname'];
echo $name;
?>
<html>
<body>
<form method="post">
Name: <input type="text" name="fname">
<input type="submit">
</form>
</body>
</html>
PHP $_GET
PHP $_GET can also be used to collect form data after
submitting an HTML form with method="get".
$_GET can also collect data sent in the URL.
Example:
<?php
$name = $_POST['fname'];
echo $name; ?>
<html>
<body>
<form method="post">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<a href=“test.php?name=ali">Name</a>
</body>
</html>
In test.php simply write <?php echo $_GET[‘ali’]; ?>
PHP Form Handling
The PHP superglobals $_GET and $_POST are used to collect form-
data.
GET:
Information sent from a form with the GET method is visible to
everyone (all variable names and values are displayed in the URL).
GET may be used for sending non-sensitive data.
Note: GET should NEVER be used for sending passwords or other
sensitive information!
POST
Information sent from a form with the POST method
is invisible to others (all names/values are embedded
within the body of the HTTP request).
Moreover POST supports advanced functionality such
as support for multi-part binary input while uploading
files to server.
Developers prefer POST for sending form data.