Creating Functions within Functions


You can even nest functions in PHP. However, much as with conditional functions, the internal function isn't seen by PHP until the enclosing function is called. You can see an example in phpnestedfunction.php, Example 4-14, where we call a function within a function, but only after the enclosing function is called.

Example 4-14. Using variable functions, phpnestedfunction.php
 <HTML>         <HEAD>             <TITLE>Nesting Functions</TITLE>         </HEAD>         <BODY>             <H1>Nesting Functions</H1>             <?php                 function enclosing_function()                 {                     echo "Hello from the enclosing function!<BR>";                     function created_function()                     {                         echo "Hello from the nested function!<BR>";                     }                 }                 enclosing_function();                 created_function();         ?>     </BODY> </HTML> 

The results appear in Figure 4-14, where you can see that both functions have run.

Figure 4-14. Nesting functions.




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net