Bitlab
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.
*/
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.
*/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
<?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);
ASKER
Thank you, hernst42.
especially, for modification which I hope to use in form:
function top()
{
$x=1;
if(!function_exists('insid e'))
{
function inside($y)
{
echo ($y+2);
}
}
inside(2);
echo $x.' ';
}
However, lack of reference to cow path makes us feel less secure.
B
especially, for modification which I hope to use in form:
function top()
{
$x=1;
if(!function_exists('insid
{
function inside($y)
{
echo ($y+2);
}
}
inside(2);
echo $x.' ';
}
However, lack of reference to cow path makes us feel less secure.
B
Cow path?
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