0

[CONFUSED!] One of the challenges in PHP is confusing me🤔

It's like this; function a(){ function b(){ Echo 'I am b'; } Echo 'I am a'; } a(); a(); ANS: I am a Fatal error... Please....Where does "Fatal Error" come from? Is anyone else confused by other challenges other than this one??

1st Jul 2020, 11:08 PM
Iragaba Ramathan
Iragaba Ramathan - avatar
4 ответов
+ 4
The "I am a" part of the output comes from running the a() function the first time. The first run of a() will also declare the b() function so that it will now exist in the global scope. Only after calling a() can you call b(), but then you can not call a() again without it resulting in the Fatal error which is the remaining portion of the output. This error occurs because it is trying to re-declare the b() function. Read here under the 'lifetimes' section and it will describe it better and how to avoid the issue. https://www.i-programmer.info/programming/php/1130-php-inner-functions-and-closure.html
1st Jul 2020, 11:49 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
when the function is called to (); it runs and prints i am a, then b () should be called because it is part of the function, but again a () is called when b () should be called; for that reason the fatal error comes out
2nd Jul 2020, 12:21 AM
Reivin
Reivin - avatar
+ 1
Thank You Guys for the help..really means alot
2nd Jul 2020, 1:38 AM
Iragaba Ramathan
Iragaba Ramathan - avatar
0
Interesting read, is this PHP specific? Because as far as I know, you cannot declare a function inside a function in Java or C#
2nd Jul 2020, 9:21 AM
HNNX 🐿
HNNX 🐿 - avatar