PHP – $_REQUEST

$_REQUEST

$_REQUEST is a PHP super global variable which contains submitted form data, and all cookie data.

In other words, $_REQUEST is an array containing data from $_GET, $_POST, and $_COOKIE.

You can access this data with the $_REQUEST keyword followed by the name of the form field, or cookie, like this:

$_REQUEST['firstname']

Continue reading PHP – $_REQUEST

PHP – $_SERVER

$_SERVER

$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.

The example below shows how to use some of the elements in $_SERVER:

Example

echo $_SERVER['PHP_SELF'];
echo $_SERVER['SERVER_NAME'];
echo $_SERVER['HTTP_HOST'];
echo $_SERVER['HTTP_REFERER'];
echo $_SERVER['HTTP_USER_AGENT'];
echo $_SERVER['SCRIPT_NAME'];

The following table lists the most important elements that can go inside $_SERVER: Continue reading PHP – $_SERVER

PHP $GLOBALS

$GLOBALS is an array that contains all global variables.

Global Variables

Global variables are variables that can be accessed from any scope.

Variables of the outer most scope are automatically global variables, and can be used by any scope, e.g. inside a function.

To use a global variable inside a function you have to either define them as global with the global keyword, or refer to them by using the $GLOBALS syntax. Continue reading PHP $GLOBALS

PHP Global Variables – Superglobals

Superglobals were introduced in PHP 4.1.0, and are built-in variables that are always available in all scopes.

PHP Global Variables – Superglobals

Some predefined variables in PHP are “superglobals”, which means that they are always accessible, regardless of scope – and you can access them from any function, class or file without having to do anything special. Continue reading PHP Global Variables – Superglobals

PHP Array Functions

PHP Array Functions

PHP has a set of built-in functions that you can use on arrays. Continue reading PHP Array Functions

PHP Multidimensional Arrays

In the previous pages, we have described arrays that are a single list of key/value pairs.

However, sometimes you want to store values with more than one key. For this, we have multidimensional arrays. Continue reading PHP Multidimensional Arrays

PHP Sorting Arrays

The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.

PHP – Sort Functions For Arrays

In this chapter, we will go through the following PHP array sort functions:

  • 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

Continue reading PHP Sorting Arrays

PHP Delete Array Items

Remove Array Item

To remove an existing item from an array, you can use the array_splice() function.

With the array_splice() function you specify the index (where to start) and how many items you want to delete.

Example

Remove the second item:

$cars = array("Volvo", "BMW", "Toyota");
array_splice($cars, 1, 1);

After the deletion, the array gets reindexed automatically, starting at index 0. Continue reading PHP Delete Array Items

PHP Add Array Items

Add Array Item

To add items to an existing array, you can use the bracket [] syntax.

Example

Add one more item to the fruits array:

$fruits = array("Apple", "Banana", "Cherry");
$fruits[] = "Orange";

Continue reading PHP Add Array Items

PHP Update Array Items

Update Array Item

To update an existing array item, you can refer to the index number for indexed arrays, and the key name for associative arrays.

Example

Change the second array item from “BMW” to “Ford”:

$cars = array("Volvo", "BMW", "Toyota");
$cars[1] = "Ford";

Note: The first item has index 0.

Continue reading PHP Update Array Items