PHP Function Reference

PHP get_defined_vars() Function



The PHP get_defined_vars() function returns a multidimensional array containing a list of all defined variables, environment, server or user-defined variables, within the scope in which this function is called.

Syntax

get_defined_vars()

Parameters

No parameter is required.

Return Value

Returns a multidimensional array with all the variables.

Example:

The example below shows the usage of get_defined_vars() function.

<?php
$x = array(1, 2, 3, 4, 5);

$result = get_defined_vars();

//displaying the result array
print_r($result);
?>

The output of the above code will be:

Array
(
    [_GET] => Array
        (
        )

    [_POST] => Array
        (
        )

    [_COOKIE] => Array
        (
        )

    [_FILES] => Array
        (
        )

    [argv] => Array
        (
            [0] => Main.php
        )

    [argc] => 1
    [_SERVER] => Array
        (
            [APACHE_RUN_DIR] => /var/run/apache2
            [APACHE_PID_FILE] => /var/run/apache2/apache2.pid
            [JOURNAL_STREAM] => 8:22712
            [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
            [INVOCATION_ID] => 135fb1968e1a4ae6a2fa49896ea1d5a4
            [APACHE_LOCK_DIR] => /var/lock/apache2
            [LANG] => C
            [APACHE_RUN_USER] => www-data
            [APACHE_RUN_GROUP] => www-data
            [APACHE_LOG_DIR] => /var/log/apache2
            [PWD] => 
            [PHP_SELF] => Main.php
            [SCRIPT_NAME] => Main.php
            [SCRIPT_FILENAME] => Main.php
            [PATH_TRANSLATED] => Main.php
            [DOCUMENT_ROOT] => 
            [REQUEST_TIME_FLOAT] => 1630049839.4612
            [REQUEST_TIME] => 1630049839
            [argv] => Array
                (
                    [0] => Main.php
                )

            [argc] => 1
        )

    [x] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
        )

)

❮ PHP Variable Handling Reference