473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Variable scope - function in function

Here's something that I can't manage to find explicitly documented. In the
following snippet:

function outer() {
global $a;
function inner() {
global $a;
echo $a.'1<br>';
}
$a = 's';
inner();
echo $a.'2<br>';

}
outer();
?>

If either of the globals statements is removed, the variable is not
accessable within inner.
May 21 '06 #1
6 9842

Paul Lautman wrote:
Here's something that I can't manage to find explicitly documented. In the
following snippet:

function outer() {
global $a;
function inner() {
global $a;
echo $a.'1<br>';
}
$a = 's';
inner();
echo $a.'2<br>';

}
outer();
?>

If either of the globals statements is removed, the variable is not
accessable within inner.


What you're trying to do won't work if you call outer() more than once.
PHP has no support for closure or inner functions.

May 21 '06 #2
Chung Leong wrote:
Paul Lautman wrote:
Here's something that I can't manage to find explicitly documented.
In the following snippet:

function outer() {
global $a;
function inner() {
global $a;
echo $a.'1<br>';
}
$a = 's';
inner();
echo $a.'2<br>';

}
outer();


If either of the globals statements is removed, the variable is not
accessable within inner.


What you're trying to do won't work if you call outer() more than
once. PHP has no support for closure or inner functions.


Interesting, that case indeed fails. Yet I came across this problem when
defining a sort function for usort. In that case, one call call the sort
function more than once.
May 21 '06 #3
Rik
Chung Leong wrote:
Paul Lautman wrote:
Here's something that I can't manage to find explicitly documented.
In the following snippet:

function outer() {
global $a;
function inner() {
global $a;
echo $a.'1<br>';
}
$a = 's';
inner();
echo $a.'2<br>';

}
outer();

If either of the globals statements is removed, the variable is not
accessable within inner.


Which is abolutely logical.
What you're trying to do won't work if you call outer() more than
once.
PHP has no support for closure or inner functions.

You could circumvent this by:
if(!function_exists('inner'){
//define function
}

I'm very curious though why one would need such an imho messy function
declaration.

Grtz,
--
Rik Wasmus
May 21 '06 #4
On Sun, 21 May 2006 21:41:27 +0100, Paul Lautman wrote:
Chung Leong wrote:
Paul Lautman wrote:
Here's something that I can't manage to find explicitly documented.
In the following snippet:

function outer() {
global $a;
function inner() {
global $a;
echo $a.'1<br>';
}
$a = 's';
inner();
echo $a.'2<br>';

}
outer();
If either of the globals statements is removed, the variable is not
accessable within inner.


What you're trying to do won't work if you call outer() more than
once. PHP has no support for closure or inner functions.


Interesting, that case indeed fails. Yet I came across this problem when
defining a sort function for usort. In that case, one call call the sort
function more than once.

Well, it fails complaining about the duplicate declaration, not about the
invocation. This version will work:

#!/usr/local/bin/php
<?php
$a="A";
function outer() {
global $a;
if ($a=="A") {
function inner() {
global $a;
echo $a."1\n";
}
}
$a = "s";
inner();
echo $a."2\n";

}
outer();
outer();
?>
$

This way, the inner function is not re-declared during the second
invocation. This can also be simulated by using eval.
--
http://www.mgogala.com

May 22 '06 #5
Rik wrote:
Chung Leong wrote:
Paul Lautman wrote:
Here's something that I can't manage to find explicitly documented.
In the following snippet:

function outer() {
global $a;
function inner() {
global $a;
echo $a.'1<br>';
}
$a = 's';
inner();
echo $a.'2<br>';

}
outer();
If either of the globals statements is removed, the variable is not
accessable within inner.


Which is abolutely logical.
What you're trying to do won't work if you call outer() more than
once.
PHP has no support for closure or inner functions.

You could circumvent this by:
if(!function_exists('inner'){
//define function
}

I'm very curious though why one would need such an imho messy function
declaration.

Grtz,


I don't actually need it. What happened was that I was having trouble
getting a variable to be global so that I could use a single callback
function for my usort and have it sort on different "fields" in my array. It
worked OK in a test setup, but not in the application. Turns out that I
hadn't realised that in the application, the callback function was declared
inside another function, whereas in the test setup it was at the top level.
May 22 '06 #6
Rik
Paul Lautman wrote:
What you're trying to do won't work if you call outer() more than
once.
PHP has no support for closure or inner functions.

You could circumvent this by:
if(!function_exists('inner'){
//define function
}

I'm very curious though why one would need such an imho messy
function declaration.

Grtz,


I don't actually need it. What happened was that I was having trouble
getting a variable to be global so that I could use a single callback
function for my usort and have it sort on different "fields" in my
array. It worked OK in a test setup, but not in the application.
Turns out that I hadn't realised that in the application, the
callback function was declared inside another function, whereas in
the test setup it was at the top level.

Check, while nesting more and more it becomes hard to find errors like that.
I always try to keep "home-made" funstions in a seperate include for that
reason.

Grtz,
--
Rik Wasmus
May 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: manish | last post by:
I have created a function, it gives more readability compared to the print_r function. As of print_r, it works both for array or single variable. I just want to add in it, the opton to view the...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
14
by: rahul8143 | last post by:
hello, To limit scope of a variable in a single file that is part of a large project that have several C files we use static variable right?then to limit any variable to function scope it should...
6
by: Maarten | last post by:
Here is a nice problem. I have two scripts, one calling the other with via an include. The script that is included contains a variable, a function and a call to that function. The variable needs to...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
3
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.