Académique Documents
Professionnel Documents
Culture Documents
By
Shahab Ul Islam
PHP Arrays
An array is a special variable, which can hold more
than one value at a time.
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.
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
$shop=array(
“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).
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.