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