0

I'm not in OOP and I would like understand why in the procedural mode I can declare a function nestled in a function without errors, but I can call the nestled function from the "main" and cannot call from the primary function?

Example 1: calling b() in a() gives Fatal error / Why a() doesn't views b() ?

<?php
function a(){
 // do something
 b(); //Fatal error: Call to undefined function b()
 function b(){
 // do something
 }
}
a();

Example 2: calling b() from the main gives Fatal error (this is logic)

<?php
function a(){
 // do something
 function b(){
 // do something
 }
} 
b(); // Fatal error: Call to undefined function b()

Example 3: calling a() and then calling b() from the main doesn't give error

<?php
function a(){
 // do something
 function b(){
 // do something
 }
}
a();
b();
asked Mar 27, 2018 at 13:26
6
  • probably because your syntax is incorrect Commented Mar 27, 2018 at 13:28
  • 1
    Just follow the flow of the script, the function is still undefined when you try to call it in #1 and #2. By the way, this is bound to give you trouble later on as you can only call a() once during the whole execution of your script. Commented Mar 27, 2018 at 13:29
  • 1
    Nesting functions in PHP is rarely what you want to do - it means that b() is only declared once you first call a(), and will raise an error about redeclaring an existing function if you ever call a() again. Both functions will be declared in global scope, which isn't necessarily obvious. Commented Mar 27, 2018 at 13:32
  • @iainn surely, but suppose I need to use the function b() only in the function a() ... probably it shall be better use a LAMBDA function I suppose Commented Mar 27, 2018 at 13:40
  • @ViDiVe Yes, you should absolutely use a locally scoped anonymous function in that case. Having the functions nested as you have will cause issues with both scope and re-usability. Commented Mar 27, 2018 at 13:52

1 Answer 1

3

PHP is a procedural programming language. It will do each line, in order. The reason you can't call b() from within a() is because, at that point, b() has not been declared. What you want to do is declare your function before calling it:

<?php
function a(){
 // do something
 function b(){
 // do something
 }
 b(); 
}
a();

This is still bad practice though. Break b() out of a() :

<?php
function a(){
 // do something
 b();
}
function b(){
 // do something
}
a();

This will allow you to call a() and b() at any time.

answered Mar 27, 2018 at 13:31
1
  • Thanks, now I've understood! Commented Mar 27, 2018 at 13:35

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.