php

List of Superglobal Variables in PHP

Superglobal variables are variables that can be accessed from anywhere in the script at any time. So they are within the global scope and the use of the keyword “global” or access via $GLOBALS is not necessary. Probably the most important superglobal variables in the list are $_GET and $_POST, which contain data sent by the user. Precisely there are the following noteworthy superglobals :

$_GET: All request parameters from the URL. For example, for the URL http://www.example.com/page.php?foo=bar&test=123 the GET parameters are “foo” (with value “bar”) and “test” (with value “123”)

$_POST: All parameters sent by HTTP-POST. Usually these are sent by the user when he submits a filled out form with the “method” attribute set to “post”.

$GLOBALS: All global variables defined in the script are placed in this array.

$_SERVER: All data generated by the webserver, such as the URL called, the user’s IP, or file paths.

$_COOKIE: Cookies sent by the visitor.

$_SESSION: The data of the visitor’s session. A session is used to save data across multiple page views.

$_FILES: files sent by the visitor.
 

PHP MCQ - Multiple Choice Questions and AnswersPHP Questions and Answers – Object-Oriented OOP – Part 1This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Object-Oriented OOP”.   1. The concept of…Read More
Example : $_GET

Assuming that a file test.php exists and this is called with test.php?name=Alex&age=20:

<?php
    // File called via test.php?name=alex&age=20

    // Output of all GET parameters
    var_dump($_GET);
     
    // You can also access specific parameters
    echo('Name: ' . $_GET['name'] . "\n");
    echo('Age: ' . $_GET['age']);
?>

Output:

array(3) {
  ["name"]=>
  string(3) "Alex"
  ["age"]=>
  string(3) "20"
}
Name: Alex
Age: 20

 

Example : $_POST

The following example shows a short form that sends its data via POST. After submitting the form the content of the input fields are displayed.

<form action="" method="post">
    <input type="text" name="name" value="Alex" />
    <input type="text" name="age" value="20" />
    <input type="submit" />
</form>
<?php
    if (isset($_POST['name']) && isset($_POST['age'])) {
        echo('Name: ' . $_POST['name'] . "\n");
        echo('Age: ' . $_POST['age'] . "\n");
    }
?>

Output after sending the form:

Name: Alex
Age: 20

 

PHP global variablePHP global variable with ExampleVariables usually have a scope, so variables defined in one function are not available for other functions. However, it is also possible to place variables…Read More
Example : $GLOBALS

In this example, a variable is written to the $GLOBALS array and retrieved by the displayMyGlobal() function, which normally would not have access to this variable if it were not in the $GLOBALS array.

<?php
    $GLOBALS['myGlobal'] = 'test';
 
    function displayMyGlobal() {
        echo('myGlobal: ' . $GLOBALS['myGlobal'] . "\n");
    }
 
    displayMyGlobal();
?>

Output:

myGlobal: test

 

Example : $_SERVER

In this example, the superglobal variable $_SERVER is used to get information about the caller’s IP address, the call method and the current PHP file.

<?php
    var_dump($_SERVER['REMOTE_ADDR']);
    var_dump($_SERVER['REQUEST_METHOD']);
    var_dump($_SERVER['PHP_SELF']);
?>

Output:

string(9) "127.0.0.1"
string(3) "GET"
string(12) "/example.php"

 

 

Example : $_COOKIE

A cookie is set using setcookie(), which in turn is read out using $_COOKIE (a super global). The cookie is set on the first page request and therefore cannot be read from $_COOKIE. On the second visit it is available (provided that cookies are activated for the visitor).

<?php
    setcookie('mycookie', 'value_of_cookie');
 
    if (isset($_COOKIE['mycookie'])) {
        echo('mycookie: ' . $_COOKIE['mycookie']);
    } else {
        echo('mycookie not found.');
    }
?>

Output on first page view:

mycookie not found.

Output on second page view:

mycookie: value_of_cookie
Difference between Include and Require in PHPDifference between Include and Require in PHPTo include other files containing PHP code in the same PHP file, the statements include and require can be used.   Example : // to…Read More
Example : $_SESSION

A $_SESSION is first started using the session_start() function. This is absolutely necessary, otherwise, the information in $_SESSION will be lost if a session has not yet been started or will not be reloaded if a session has recently been started for the visitor. After starting/loading the session, a check is made to see whether the “time” value has already been set. If not, it will be set to the current time. Otherwise, it will be issued. The first time you call up the page you only get the message that the time has been set. The time is only displayed from the second call, which remains constant for all subsequent calls. (If you simply reload the page, you must have activated cookies.)

<?php
    session_start();
 
    if (!isset($_SESSION['time'])) {
        $_SESSION['time'] = date('H:i:s');
        echo('Time set.');
    } else {
        var_dump($_SESSION['time']);
    }
?>

Output on first page view:

Time set.

Output on second page view:

string(8) "20:18:59"
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *