xref: /PHP-7.1/Zend/tests/get_defined_vars.phpt (revision 7af945e2)
1--TEST--
2Testing get_defined_vars() Function
3--FILE--
4<?php
5/* Prototype: array get_defined_vars(void);
6 * Description: Returns a  multidimentional array of all defined variables.
7 */
8
9/* Various variables definitions used for testing of the function */
10
11$number = 22.33; //number
12$string = "sample string"; //string
13$array1 = array(1, 1, 2, 3, 5, 8); //simple array
14$assoc_array = array( 'a'=>97, 'c'=>99, 'A'=>65, 'C'=>67, 1=>"string1" ); //associative array
15$boolean = TRUE; //boolean
16
17/* Checking for Class and Objects */
18class sample {
19var $number = 233;
20var $string = "string2";
21public function func() {
22$local_var = 2;
23var_dump( get_defined_vars() );
24}
25}
26$sample_obj = new sample; //object declaration
27
28function func() {
29$string33 = 22;
30var_dump( get_defined_vars() );
31}
32
33$arr = get_defined_vars();
34
35/* Displaying various variable through the array captured by the get_defined_vars function call */
36echo "\n*** Displaying various variables through the array captured by the get_defined_vars function call ***\n";
37var_dump( $arr["argc"] );
38var_dump( $arr["number"] );
39var_dump( $arr["string"] );
40var_dump( $arr["array1"] );
41var_dump( $arr["assoc_array"] );
42var_dump( $arr["boolean"] );
43var_dump( $arr["sample_obj"] );
44
45
46echo "\n*** Checking for output when get_defined_vars called in local function ***\n";
47func();
48
49
50echo "\n*** Checking for output when get_defined_vars called in function of a class ***\n";
51$sample_obj->func();
52
53echo "\n*** Checking for output when get_defined_vars called in nested functions ***\n";
54function func1(){
55$func1_var = 2;
56var_dump( get_defined_vars() );
57function func2(){
58$func2_var = 3;
59var_dump( get_defined_vars() );
60}
61func2();
62}
63func1();
64
65echo "\nDone";
66?>
67--EXPECTF--
68*** Displaying various variables through the array captured by the get_defined_vars function call ***
69int(1)
70float(22.33)
71string(13) "sample string"
72array(6) {
73  [0]=>
74  int(1)
75  [1]=>
76  int(1)
77  [2]=>
78  int(2)
79  [3]=>
80  int(3)
81  [4]=>
82  int(5)
83  [5]=>
84  int(8)
85}
86array(5) {
87  ["a"]=>
88  int(97)
89  ["c"]=>
90  int(99)
91  ["A"]=>
92  int(65)
93  ["C"]=>
94  int(67)
95  [1]=>
96  string(7) "string1"
97}
98bool(true)
99object(sample)#1 (2) {
100  ["number"]=>
101  int(233)
102  ["string"]=>
103  string(7) "string2"
104}
105
106*** Checking for output when get_defined_vars called in local function ***
107array(1) {
108  ["string33"]=>
109  int(22)
110}
111
112*** Checking for output when get_defined_vars called in function of a class ***
113array(1) {
114  ["local_var"]=>
115  int(2)
116}
117
118*** Checking for output when get_defined_vars called in nested functions ***
119array(1) {
120  ["func1_var"]=>
121  int(2)
122}
123array(1) {
124  ["func2_var"]=>
125  int(3)
126}
127
128Done
129