Avatar of Bitlab
BitlabFlag for United States of America

asked on 

Inserted functions in php syntax

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.
*/
PHP

Avatar of undefined
Last Comment
Ray Paseur
Avatar of Jagadishwor Dulal
Jagadishwor Dulal
Flag of Nepal image

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
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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

Avatar of Bitlab
Bitlab
Flag of United States of America image

ASKER

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Cow path?
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo