0% found this document useful (0 votes)
18 views

Sorting in Arrays

The document discusses various PHP functions for sorting arrays, including sort(), rsort(), asort(), ksort(), arsort(), and krsort(). It also discusses PHP superglobal variables such as $_SERVER, $_REQUEST, $_POST, $_GET, and $_FILES, which contain information about headers, paths, scripts, form data, and files. Examples are given showing how to use the asort() function to sort an associative array by value, and how to retrieve values from $_SERVER, $_REQUEST, and $_POST superglobals.

Uploaded by

Shivansh Pundir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Sorting in Arrays

The document discusses various PHP functions for sorting arrays, including sort(), rsort(), asort(), ksort(), arsort(), and krsort(). It also discusses PHP superglobal variables such as $_SERVER, $_REQUEST, $_POST, $_GET, and $_FILES, which contain information about headers, paths, scripts, form data, and files. Examples are given showing how to use the asort() function to sort an associative array by value, and how to retrieve values from $_SERVER, $_REQUEST, and $_POST superglobals.

Uploaded by

Shivansh Pundir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

SORTING IN ARRAYS

AND SUPER GLOBAL VARIABLES


BY:
PARUL MADAN(AP)
GEU
PHP - Sort Functions For
Arrays
 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
<!DOCTYPE html>
<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>
<!DOCTYPE html>
<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>
PHP Global Variables - Superglobals

 $GLOBALS
 $_SERVER
 $_REQUEST
 $_POST
 $_GET
 $_FILES
 $_ENV
 $_COOKIE
 $_SESSION
PHP $_SERVER
$_SERVER is a PHP super global variable which holds information about
headers, paths, and script locations.
For eg:
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
PHP Superglobal - $_REQUEST
PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML
form.

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname“ value=“”>
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_REQUEST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>

</body>
</html>
PHP Superglobal - $_POST
PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML
form with method="post". $_POST is also widely used to pass variables.
<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_POST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>
PHP $_GET

 PHP $_GET is a PHP super global variable


which is used to collect form data after
submitting an HTML form with method="get".

 Both GET and POST create an array (e.g.


array( key1 => value1, key2 => value2, key3 =>
value3, ...)). This array holds key/value pairs,
where keys are the names of the form controls
and values are the input data from the user.

You might also like