Originally, in PHP language some variables are predefined and can be accessed anywhere in the script, without the need to declare them. These variables are called superglobals and offer the programmer a number of important information about  customers and also about the server where the application is allocated. Following, we present some of these variables and understand how they interact within the application.

 

$_SERVER

In this array are stored information related to the server, such as name and IP address, for example. To access the information stored in the array, simply perform the request by the name of the position:

<?php
echo $_SERVER[‘SERVER_NAME’];
?>

The most diverse information can be found when using the $ _SERVER array. So all the names and values by position can be found in the official documentation of the resource.

 

$_GET

This array is used when you need to work with querystrings or variables passed via HTTP GET method. When working with querystrings the URL, you can then recover the amount from the name and use it within the local script:

<?php
echo ‘Hello ‘ . htmlspecialchars($_GET[“name”]) . ‘!’;
?>

Understanding that the user accessed the url http://example.com/?name=Hannes, the information that will be printed on screen will be: Hello Hannes!

$_POST

This array is used in cases when you need to work with querystrings or variables passed via HTTP POST method. When working with querystrings in the URL, you can recover the value from the name and use it within the local script:

<?php
echo ‘Hello ‘ . htmlspecialchars($_POST[“name”]) . ‘!’;
?>

 

Understanding that the user accessed the url http://example.com/?name=Hannes, the information that will be printed on screen will be: Hello Hannes!

 

$_COOKIE

This array accesses the information that was stored on the client through the use of cookies.

<?php
echo ‘Hello ‘ . htmlspecialchars($_COOKIE[“name”]) . ‘!’;
?>

 

Understanding that a cookie was stored on the client machine before, the example will show on screen the following information: Hello Hannes!

The presented superglobal variables are frequently used in web development environments – however, a number of others are available for any situations. To know each of them and how they can be used, just visit the official documentation available at PHP.net website.

You might also like…

Scriptcase & Web Services

This time I will explain how to work with web services inside the Scriptcase, directly with PHP and...

More updates in 9.4 version

Recently version 9.4 was released bringing a new design concept to the generated applications of Sc...

Taking a look at version 9.4

Scriptcase released its version 9.4, and this one brings many interesting news, specially regarding...

Comment this post

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.