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:
ExampleGet your own PHP Server
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'];
Try it Yourself »
The following table lists the most important...
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.
ExampleGet your own PHP Server
Refer to...
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.
The PHP superglobal variables are:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
The...
PHP Array Functions
PHP Array Functions
PHP has a set of built-in functions that you can use on arrays.
Function
Description
array()
Creates an array
array_change_key_case()
Changes all keys in an array to lowercase or uppercase
array_chunk()
Splits an array into chunks of arrays
array_column()
Returns the values from a single column in the input array
array_combine()
Creates an array by using the elements from one "keys" array and one "values" array...
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.
PHP - Multidimensional Arrays
A multidimensional array is an array containing one or more arrays.
PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.
The dimension of...
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...
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.
ExampleGet your own PHP Server
Remove the second item:
$cars = array("Volvo", "BMW", "Toyota");
array_splice($cars, 1, 1);
Try it Yourself »
After the deletion, the array gets reindexed automtically, starting at index 0.
Using the unset...
PHP Add Array Items
Add Array Item
To add items to an existing array, you can use the bracket [] syntax.
ExampleGet your own PHP Server
Add one more item to the fruits array:
$fruits = array("Apple", "Banana", "Cherry");
$fruits[] = "Orange";
Try it Yourself »
Associative Arrays
To add items to an associative array, or key/value array, use brackets [] for the key, and assign value with the = operator.
Example
Add one item to the car array:...
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.
ExampleGet your own PHP Server
Change the second array item from "BMW" to "Ford":
$cars = array("Volvo", "BMW", "Toyota");
$cars[1] = "Ford";
Try it Yourself »
Note: The first item has index 0.
To update items from an associative array, use the key name:
Example
Update the year to 2024:
$cars = array("brand"...
PHP Access Arrays
Access Array Item
To access an array item, you can refer to the index number for indexed arrays, and the key name for associative arrays.
ExampleGet your own PHP Server
Access an item by referring to its index number:
$cars = array("Volvo", "BMW", "Toyota");
echo $cars[2];
Try it Yourself »
Note: The first item has index 0.
To access items from an associative array, use the key name:
Example
Access an item by referring to its key name:
$cars =...
PHP Create Arrays
Create Array
You can create arrays by using the array() function:
ExampleGet your own PHP Server
$cars = array("Volvo", "BMW", "Toyota");
Try it Yourself »
You can also use a shorter syntax by using the [] brackets:
Example
$cars = ["Volvo", "BMW", "Toyota"];
Try it Yourself »
Multiple Lines
Line breaks are not important, so an array declaration can span multiple lines:
Example
$cars = [
"Volvo",
"BMW",
"Toyota"
];
Try it...
PHP Associative Arrays
PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
ExampleGet your own PHP Server
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
var_dump($car);
Try it Yourself »
Access Associative Arrays
To access an array item you can refer to the key name.
Example
Display the model of the car:
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
echo $car["model"];
Try it Yourself...
Blogs
Read More
PHP Shorthand if Statements
Short Hand If
To write shorter code, you can write if statements on one line....
PHP Introduction
PHP code is executed on the server.
What You Should Already Know
Before you continue you...
PHP while Loop
The while loop - Loops through a block of code as long as the specified condition is...
PHP Variables
Variables are "containers" for storing information.
Creating (Declaring) PHP Variables
In...
PHP Numbers
In this chapter we will look in depth into Integers, Floats, and Number Strings.
PHP Numbers...