https://www.tutorialspoint.com/php_webview_online.php https://www.tutorialspoint.com/php/php_functions.htm Writing PHP Function which returns value https://www.i-programmer.info/programming/php/1130-php-inner-functions-and-closure.html?start=1 When you put them together with anonymous functions, added in PHP 5.3.0, you have something that is almost, but not quite, the equal of what other languages offer. But first let's look at the basics of PHP functions. Functions are global A PHP function is simple to define: function MyFunction() { instructions that make up the function } You can include parameters and a return statement. The important point is that any function defined in this way has global scope. It can be called from anywhere within the entire program including from within another function and even from within a class or an instance of a class. For example, if we define the function: function MyFunction() { echo("My Function"); } Then it can be used from within a class: class MyClass { public function MyMethod() { MyFunction(); } } and when the method is called $MyObject=new MyClass(); $MyObject->MyMethod(); MyFunction is called. This might seem perfectly normal to you as a PHP programmer but most other object oriented languages don't even allow you to define a function outside of a class, i.e. all functions are methods, so the idea of having global functions doesn't arise. Using functions in this way is not a good idea even though it can be rationalised as providing "helper" functions to the objects you create. In general the functionality of a class should always be contained within the class and not made available by a global entity. You can argue that string functions are an example of utility functions that should be global, for example count_chars is a global function that returns the number of characters in a string, but is just an indication that you are not being object oriented enough. If a function does something to a string, i.e. it's a string operation, then it should be part of a string class. To find any convincing examples of functions that should be global you have to look at either operations that involve more than one type - which class should the function belong to - or a where a single function works with multiple types. Working with multiple types is usually handled in fully object oriented languages by using generics but loosely typed languages such as PHP just allow you to ignore the problems of type and write a global function. It sort of works and avoids introducing a complicated idea into the language. Inner functions If you define a variable within a function it is local to that function. You can also define a function within another function and logically this too should be a local variable but recall the rule that all functions are global. In PHP inner functions are global and hence behave in the same way as if they had been declared outside of any containing function. What this means is that inner functions are useless. As they are global they have the same context as a function declared in the usual way and this in turn means that they cannot support any of the clever tricks that other languages provide such as closure. Closure is where an inner function has access to the local variables of the function that contains it - even if the outer function no longer exists. As inner functions are global they have no privileged access to the local variables of the functions in which they are declared. Also as functions are global they never go out of scope and so are never destroyed. Thus PHP functions miss two things that make closure possible and a useful feature and both are due to the the functions being global. o be clear: an inner function behaves in exactly the same way as if it had been declared outside of its containing function. It has no access to the local variables of its containing function and concepts such as closure simply do not apply. Lifetimes The fact that functions are global means that they don't go out of scope which means that they are never destroyed. But this doesn't mean that every function has the same lifetime as the program they are contained in. Any function that you simply declare as part of the program's text can be considered to exist as soon as the program starts running and exist until the program comes to an end. However, PHP is a dynamic interpreted language and this means that it can do things with its own text that other languages can't. For example you can conditionally declare a function: if($test) { function MyFunction() { echo("My Function"); } } This results in MyFunction not existing within the program until the if statement is executed and the condition is true. Once this has happened the function exists and continues to exist until the end of the program. You can think of this as "the function exists from the moment that PHP gets to read about it". This is a completely general mechanism and applies to any declaration of a function within other PHP code. Notice that as a function cannot be redefined within PHP this means that any code that declares a function can only be executed once - or you have to make sure that the logic is such that the function isn't created a second time. It also applies to inner functions and this means that an inner function doesn't exist until the outer function has been called. This some how seems much stranger than the conditional example given earlier but it really is just the same principle at work. For example, if you define two functions, one within the other: function MyFunction() { echo("My Function"); function MyInnerFunction() { echo('MyInnerFunction '); } } Then if you simply call MyInnerFunction() it will fail. To make it work you first have to call the outer function and only then does the inner function exist. That is: MyFunction(); MyInnerFunction(); works but after calling MyFunction once you can't call it again without generating an error. The solution to the problem of trying to create a function more than once is to make use of the conditional declaration idea. For example: function MyFunction() { echo("My Function"); if(!function_exists("MyInnerFunction")){ function MyInnerFunction() { echo('MyInnerFunction '); } } } The way that this works should now be obvious. The if statement simply checks to see if the function name exists in the global scope. If it does then the function isn't declared a second time and no error occurs. You should always enclose inner function declarations within if statements - it makes them safer and it costs very little. Now we come to a subtle point but one that turns out to have practical import. Can you call an inner function from within the function that encloses it? The answer is yes but only after it has been defined. For example; you can't use function MyFunction() { echo("My Function"); MyInnerFunction(); if(!function_exists("MyInnerFunction")){ function MyInnerFunction() { echo('MyInnerFunction '); } } } The problem is that when you try to call MyInnerFunction it hasn't been declared yet. However if you move the call to after the if then everything is fine: function MyFunction() { echo("My Function"); if(!function_exists("MyInnerFunction")){ function MyInnerFunction() { echo('MyInnerFunction '); } } MyInnerFunction(); } This is strange and takes some time to get used to but it is perfectly logical. Of course the second time MyFunction is called the inner function is already defined and you could in principle call the inner function anywhere you like - as the inner function is global really anywhere you like! Objects and inner functions There is an interaction between object oriented programming and inner functions that we need to be aware of. It is the case that things tend to go a little wrong or strange where the non-object oriented facilities in a language meet the grafted on object oriented facilities. In the case of PHP and inner functions the logic all still works but it results in behaviour you might not expect. Consider an inner function defined within the method of a class. class MyClass { public $MyProperty; public function MyMethod() { if(!function_exists("MyInnerFunction")){ function MyInnerFunction() { echo('MyInnerFunction '); } } } } In this case the inner function is once again a global function. It certainly isn't a method of MyClass and you can't call it using object notation: $MyObject->MyInnerMethod(); To create the global function you have to call the method and then the function it created: $MyObject=new MyClass; $MyObject->MyMethod(); MyInnerFunction(); The inner function knows nothing of its origin within a class or the instance that created it - and this can be a problem. For example, the inner function does not have access to $this - it simply isn't part of the class. If you try: class MyClass { public $MyProperty="Default Value"; public function MyMethod() { echo($this->MyProperty); if(!function_exists("MyInnerFunction")){ function MyInnerFunction() { echo($this->MyProperty); echo('MyInnerFunction '); } } } } You will discover that an error is generated by the inner function Using $this when not in object context The standard solution for this sort of problem is to create a new variable - usually called $that - and explicitly transfer in the $this reference: function MyInnerFunction($that) { echo($that->MyProperty); echo('MyInnerFunction '); } and arrange to call MyInnerFunction as: MyInnerFunction($this); when ever the call is in an object context. This still doesn't quite work because the inner function isn't part of the class - its global - and hence the $that reference only gives you access to public methods and properties. That is $that can't give you access to private or protected members of the class - but in many cases access to public methods is sufficient. Using inner functions At this point you understand inner functions but might well be wondering what use they could possibly have. The answer is that many development frameworks - Joomla for example - divide up the task of creating extensions or simply using the framework into writing methods. So the documentation often says - create some code in a file called template.php and place it in a particular directory. What the framework then does is to include the file within a class definition. For example: class HTMLview{ public function htmlview(){ include "template.php" } } In other words by specifying that the code has to be in a particular file the framework has simplified the task to writing template code not the more complex idea of building a class. A sure sign that this is happening is the instruction to use $this within the template code. This is a good approach as long as the amount of code is small. If you have a lot of code to add to a framework in this way then there is a problem in that you want to divide it up into separate functions. You can't because you are already defining the code within a function and so declaring another function isn't allowed - expect of course it is. You can create an inner function. In fact any function you define in the include file is necessarily an inner function. From the point of view of the programmer working on the module to be included in the framework there might well be no understanding that they are working within an enclosing function and this can result in some strange and inexplicable behaviour. Of course if you dont' spot this then all sorts of things go wrong and many programmers conclude that they can't define functions within such include files. A fact that you can confirm if you scan though comments included in the comments of many frameworks which say something like "I have to repeat this code because I can't define a function" Well you can define a function to avoid repeating code as long as you follow some simple rules. The rules are: define the function conditionally as shown above to make sure that it is only created once. define any functions that you want to use at the start of the file so that they can be called from the rest of the code. you can't use $this but you can define a $that variable and pass it to your function you still can't access protected or private members using $that In some cases the restriction to only using public members is a problem but usually it isn't. Equally if you also know that the method will only ever be executed once then you can also drop the conditional declaration and simply define the function in the usual way. Also if you do this then the functions will be defined as the include file is read in and you can define them anywhere - i.e. they don't have to be at the start of the file. In short - you can use inner functions to divide up a method that you are writing as part of an include file even if the file is includes within another function or within a method. Anonymous functions can be local The reason that functions are global is simply that their name is defined at a global level. In PHP 5.30 and later there is another way to define a function however and in this case its "name" can be global or local. An anonymous function is simply a function that you assign to a standard variable. The function's lifespan is determined by the variable. When the variable is destroyed so is the function. That is to define an anonymous function you write something like: $variable=function(parameters){function body}; The function is anonymous because it doesn't have a function name that identifies it uniquely. Instead you can think of a reference to the function being stored in the variable and this acts as a name for the function. For example: $MyFunction1 = function() { echo('MyFunction '); }; $MyFunction2=$MyFunction1; $MyFunction1(); $MyFunction2(); Notice that you can use the reference to the anonymous function just as if it was a variable holding a value. In this case we store another reference to the anonymous function in MyFunction2. You can now call the function using either variable - the function doesn't have a fixed "name". What this means is that now an inner anonymous function, stored in a local variable is indeed local. For example function MyFunction(){ echo("My Function"); $MyInnerFunction=function() { echo('MyInnerFunction '); }; } This defines an inner anonymous function and you can call the function usng the variable as: $MyInnerFunction(); Notice that as the variable is local you cannot call the function from any other context than the outer function. Also as soon as MyFunction finishes the local variable is destroyed and the inner anonymous function effectively no longer exists. The good news is that not only is the anonymous function local to the containing function it is also subject to closure in that it can access the containing functions variables even if the containing function no longer exists.