Function inside function in PHP

I am having problems getting a function inside a functioning function, do you think that what I have below is done? I am not getting the expected results if you could shed light on the functions inside the functions that I could use.

thanks

function test1 () { global x; $x=123; function test2() { echo $x; } test2(); } 
+1
source share
3 answers

It works, but the scope of test2() limited. For example, this works:

 [ wally@zf ~]$ cat y.php <?php function test1 () { global $x; $x=123; function test2() { global $x; echo $x; } test2(); } test1(); ?> [ wally@zf ~]$ php -f y.php 123[ wally@zf ~]$ 
+3
source

Can you include it as another function outside the first function (test1)? I am having problems with the use case image.

0
source

You do not call test2, so there is no reason for it to echo $ x.

In addition, you must build the function outside, in this case there is no added value.

0
source

Source: https://habr.com/ru/post/1311385/


All Articles