Inserted functions in php syntax

Bitlab
Bitlab used Ask the Experts™
on
Hello Experts.

Is the following syntax LEGAL in php 5.1+ ?

<?php

function top()
{
      $x=1;
      function inside($y)
      {
            echo ($y+2);
      }
      inside(2);
      echo $x.' ';
}

top();
inside(5);

//outputs:41 7
/*
      Note, we don't ask works this code or not, it works.
      Note, we need some prove like reference to documentation, or to cow path in solid php packages.
      
      "Cow path" by definition is a practice of multiple programmers which is impossible to neglect by language developers.

      Here is the reason why this question.
      We are working with application which requires to write code in templates.
      Templates are php files, but they can be parts of some unknown code, so perhaps code inside of function body or class body.
    We need to isolate our local variables in "inside" function.
      We are running php 5.1 and higher.
*/
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jagadishwor DulalSenior Web Developer

Commented:
Yes it works, here I break your code in various part and now some example here
<?php
function top()
{
      $x=1;
      function inside($y)
      {
            echo ($y+2)."<br/>";
      }
      inside(2);
      echo $x.' ';
}

top();
echo inside(5);
?>

Open in new window

When I try using break in echo line of inside function it will display like:
4 //Here 4 is the first function which was passed value by reference in inside(2) to inside($y);
1 7 //Now the one is default value or value by value $x=1 and 7 is from outside of top function inside(5) which is again value by reference in inside($y) function

So you can understand that you can pass value in function by reference or by value here you already used.

For your reference you can see in:
http://www.php.net/manual/en/functions.arguments.php
Top Expert 2007
Commented:
Code only work if top is only called once, else you will get a fatal error, function already defined.
So this example is basicly a function in a function. See http://www.php.net/manual/en/functions.user-defined.php (Example 3) Works since PHP 3.x
Most Valuable Expert 2011
Top Expert 2016

Commented:
Is the following syntax LEGAL in php 5.1+ ?
You can answer this question very easily.  Install this code in a PHP5 environment and run it.

The question you did not ask was answered by hernst42.

So I guess my comment would be, don't ever do something like that!

http://www.laprbass.com/RAY_temp_bitlab.php
Outputs:
4
1 7
Fatal error: Cannot redeclare inside() (previously declared in /home/websitet/public_html/RAY_temp_bitlab.php:6) in /home/websitet/public_html/RAY_temp_bitlab.php on line 6
<?php // RAY_temp_bitlab.php
error_reporting(E_ALL);
function top()
{
      $x=1;
      function inside($y)
      {
            echo ($y+2)."<br/>";
      }
      inside(2);
      echo $x.' ';
}

top();
echo inside(5);

top();
echo inside(6);

Open in new window

Author

Commented:
Thank you, hernst42.

especially, for modification which I hope to use in form:

function top()
{
      $x=1;
      if(!function_exists('inside'))
      {
      function inside($y)
      {
            echo ($y+2);
      }
      }
    inside(2);
    echo $x.' ';
}


However, lack of reference to cow path makes us feel less secure.

B
Most Valuable Expert 2011
Top Expert 2016

Commented:
Cow path?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial