1

I'm trying to visualize how javascript and php treat nested function.

The point is this:

php:

b(); //CANNOT call b at this point because isn't defined yet
a(); //CAN call a at this point because the interpreter see the declar
b(); //ok now i can call b, because the interpreter see the declaration after a execution
function a(){
 function b(){
 echo "inner";
 }
}

meanwhile in javascript:

b(); //CANNOT call b isn't defined yet
a(); //CAN call a at this point because the interpreter see the declar
function a(){
 function b(){
 console.log("inner");
 }
}
a(); //ok now i can call a because is defined
b(); // **CANNOT** call b yet !! 

why in javascript i can't call b() even if a is executed? in what PHP act different?

Thank's in advance!

asked Jun 24, 2019 at 17:25
2
  • Even if the syntax looks similar these 2 language are completely different. JS is prototypical and has different scoping rules than PHP. Commented Jun 24, 2019 at 17:28
  • 2
    JavaScript has strict lexical scoping. Any symbol declared inside a function is private to that function (and its lexical descendents). With let and const, that also applies to simple blocks. Commented Jun 24, 2019 at 17:28

1 Answer 1

1

Its a scope thing. You could have as easily - in javascript - written "var b = function()". "b" is just a variable of type function defined within the scope of the function a. In PHP, both "a" and "b" are global functions, but it's the job of function "a" to define "b", so it won't get defined until "a" is called. Consider this example...

function a($x) {
 if ($x) {
 function b() { echo "x not empty"; }
 } else {
 function b() { echo "x empty"; }
 }
}
a(1); // Defines function b
b(); // x not empty
a(0); // PHP Fatal error: Cannot redeclare b() (previously declared...

You can see by the failure to redefine "b", that "b" is a real, globally scoped function. Function "a" could use various criteria to define the function for a particular purpose in different runs. Clearly, in this case, it wouldn't make sense to call function "b" before function "a" has decided how to define it.

I don't, by the way, think the example above is very good coding practice, but it does serve to illustrate the point.

The PHP code most similar to your javascript code would be:

function a() {
 $b = function() {
 echo "'b' says inner";
 };
 $b(); // Demonstrating the function can be used inside "a"
}
a(); // 'b' says inner

$b is a variable of type function, which can only be used within function "a".

answered Jun 24, 2019 at 23:24
3
  • Ok thank's! the point of my question was the difference about scoping of the inners function and the role of interpreter in this! Is more clear now! Commented Jun 25, 2019 at 8:18
  • @artas Why languages are what they are tends to become a bit of an existential discussion. You can understand javascript a bit better if you get back to its roots. Google "syntactic sugar and javascript" and you find a lot of modern javascript is just that. Without it javascript becomes less fun to write and harder to read, but you do get a deeper understanding of what it's doing. PHP is, well, PHP - no particular rhyme or reason to why its needle in a haystack in one function and haystack with a needle in another. You just have to learn it all... Commented Jul 4, 2019 at 14:59
  • thank's for you answer! yes I know that but my question isn't about "why" but about "how" the scoping are different. in particular how php and javascript differ in treat definition of function inside another function! Commented Jul 4, 2019 at 16:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.