0

How to get the list of defined variables and its values. eg define('DS', DIRECTORY_SEPARATOR);

How can I list all of them at the end of page to check all of them working as expected. Instead of printing all of them one by one.

I've tried get_defined_vars() But I'm getting all Super Global variables like below.

Array
(
    [GLOBALS] => Array
        (
            [GLOBALS] => Array
 *RECURSION*
            [_POST] => Array
                (
                )

            [_GET] => Array
                (
                )

            [_COOKIE] => Array
                (
                    [PHPSESSID] => tkv7odk47idt4r2ob7389tkr81
                    [CAKEPHP] => cep9tbooimh5kbhn8jovmaqgi1
                )

            [_FILES] => Array
                (
                )

            [_SERVER] => Array

But I want only those which I've mentioned using the define() statement.

Is there any way of getting it.

1
  • 1
    For the record, define defines constants, not variables. Commented Oct 20, 2012 at 4:24

4 Answers 4

2

You can get this by using get_defined_constants

$constarray = get_defined_constants(true);
foreach($constarray['user'] as $key => $val)
    eval(sprintf('$_CONSTANTS[\'%s\'] = \'%s\';', addslashes($key), addslashes($val)));
Sign up to request clarification or add additional context in comments.

Comments

2

To retrieve all the defined constants in php script you can use the function get_defined_constants()

It will return an associative array of all constants defined in php script

For more documentation about get_defined_constants please refer the below url http://php.net/manual/en/function.get-defined-constants.php

Comments

1

Use get_defined_constants(); , refer http://php.net/manual/en/function.get-defined-constants.php

Comments

0

I use this code to get all define . you can include php file and use this code

              include("FILE.PHP");  

              GET_ALL_define();

            function GET_ALL_define(){

                    $getdefinedconstants = get_defined_constants(true);
                    $definedvars = $getdefinedconstants['user'];

                    foreach($definedvars as $key => $value){
                        echo $key ." = ". $value . "<br>";
                    }

            } 

or without include php file like this code .

          define("_A","A1");
          define("_B","B2");   
          define("_C","C3");

              GET_ALL_define();

            function GET_ALL_define(){

                    $getdefinedconstants = get_defined_constants(true);
                    $definedvars = $getdefinedconstants['user'];

                    foreach($definedvars as $key => $value){
                        echo $key ." = ". $value . "<br>";
                    }

            } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.