function foo()
{
global $x;
foreach($x as $p) // fails with invalid type
{
}
}
?>
---------
<?php
$x=array();
global $x;
function foo()
{
global $x;
foreach($x as $p) // works??
{
}
}
?>
---------
This behaviour seems only limited to arrays...
--
Ineptocracy
(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.]]>The Natural Philosoph2014-02-23T18:33:52-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=184993&th=123427#msg_184993
> Consider
> <?php
> $x=array();
>
> function foo()
> {
> global $x;
> foreach($x as $p) // fails with invalid type
> {
> }
> }
> ?>
> ---------
>
> <?php
> $x=array();
> global $x;
> function foo()
> {
> global $x;
> foreach($x as $p) // works??
> {
> }
> }
> ?>
> ---------
> This behaviour seems only limited to arrays...
Just a guess, but shouldn't global be outside of the function?
In Liberty Basic, global is always placed outside of all other functions.
Then once you have it declared as global, it is not nexessary to repeat.
global $x;
function foo()
{foreach ($x as $p); }]]>Mr Oldies2014-02-23T18:40:59-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=184994&th=123427#msg_184994
> Consider
> <?php
> $x=array();
>
> function foo()
> {
> global $x;
> foreach($x as $p) // fails with invalid type
> {
> }
> }
> ?>
> ---------
>
> <?php
> $x=array();
> global $x;
> function foo()
> {
> global $x;
> foreach($x as $p) // works??
> {
> }
> }
> ?>
> ---------
> This behaviour seems only limited to arrays...
No (unless there's a bug in a particular PHP version). Your first code
sample works fine without even a notice.
Using a global statement in the global scope is not wrong, but it's
useless in this case. See the PHP manual on global[1], especially the
note at the end of the section.
--
Christoph M. Becker]]>Christoph Michael Bec2014-02-23T18:59:26-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=184995&th=123427#msg_184995
tnp@invalid.invalid> writes:
> Consider
> <?php
> $x=array();
>
> function foo()
> {
> global $x;
> foreach($x as $p) // fails with invalid type
> {
> }
> }
> ?>
foo isn't called in this example, so what you've posted is not the
actual code that's failing. The code will fail if the call to foo comes
too early, specifically before the assignment happens, but maybe you can
post a full example that fails?
<snip>
--
Ben.]]>Ben Bacarisse2014-02-23T19:16:49-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185000&th=123427#msg_185000
> The Natural Philosopher wrote:
>
>> Consider
>> <?php
>> $x=array();
>>
>> function foo()
>> {
>> global $x;
>> foreach($x as $p) // fails with invalid type
>> {
>> }
>> }
>> ?>
>> ---------
>>
>> <?php
>> $x=array();
>> global $x;
>> function foo()
>> {
>> global $x;
>> foreach($x as $p) // works??
>> {
>> }
>> }
>> ?>
>> ---------
>> This behaviour seems only limited to arrays...
>
> No (unless there's a bug in a particular PHP version). Your first code
> sample works fine without even a notice.
>
It doesn't here, but the code is being 'eval'ed' ...
> Using a global statement in the global scope is not wrong, but it's
> useless in this case. See the PHP manual on global[1], especially the
> note at the end of the section.
In this case its the difference between the program working and not working.
(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.]]>The Natural Philosoph2014-02-23T20:35:01-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185003&th=123427#msg_185003
> The Natural Philosopher <tnp@invalid.invalid> writes:
>
>> Consider
>> <?php
>> $x=array();
>>
>> function foo()
>> {
>> global $x;
>> foreach($x as $p) // fails with invalid type
>> {
>> }
>> }
>> ?>
>
> foo isn't called in this example, so what you've posted is not the
> actual code that's failing. The code will fail if the call to foo comes
> too early, specifically before the assignment happens, but maybe you can
> post a full example that fails?
>
the call to foo is after the variable is initialised.
I tested it by echoing gettype($x); both after initialising and inside
the function
It was fine outside the function and then returned NULL inside it.
I suspect the problem is in the way I am, 'eval' ing the code.
Provably the eval interpreter doesnt quite handle scope in the same way
that PHP itself does. And adding a global $x 'passes it up' to the
calling program.
> <snip>
>
--
Ineptocracy
(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.]]>The Natural Philosoph2014-02-23T20:41:48-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185004&th=123427#msg_185004
> On 23/02/14 19:16, Ben Bacarisse wrote:
>> The Natural Philosopher <tnp@invalid.invalid> writes:
>>
>>> Consider
>>> <?php
>>> $x=array();
>>>
>>> function foo()
>>> {
>>> global $x;
>>> foreach($x as $p) // fails with invalid type
>>> {
>>> }
>>> }
>>> ?>
>>
>> foo isn't called in this example, so what you've posted is not the
>> actual code that's failing. The code will fail if the call to foo comes
>> too early, specifically before the assignment happens, but maybe you can
>> post a full example that fails?
>>
>
> the call to foo is after the variable is initialised.
>
> I tested it by echoing gettype($x); both after initialising and inside
> the function
>
> It was fine outside the function and then returned NULL inside it.
>
> I suspect the problem is in the way I am, 'eval' ing the code.
Might have been a good idea to mention this in the first place. :)
> Provably the eval interpreter doesnt quite handle scope in the same way
> that PHP itself does. And adding a global $x 'passes it up' to the
> calling program.
Actually, eval() as well as include() don't change the situation, unless
they're placed in a function, as both language constructs evaluate the
code in the scope of the code using them. Apparently that is happening
in your case.
--
Christoph M. Becker]]>Christoph Michael Bec2014-02-23T20:49:54-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185006&th=123427#msg_185006
tnp@invalid.invalid> writes:
> On 23/02/14 19:16, Ben Bacarisse wrote:
>> The Natural Philosopher <tnp@invalid.invalid> writes:
>>
>>> Consider
>>> <?php
>>> $x=array();
>>>
>>> function foo()
>>> {
>>> global $x;
>>> foreach($x as $p) // fails with invalid type
>>> {
>>> }
>>> }
>>> ?>
>>
>> foo isn't called in this example, so what you've posted is not the
>> actual code that's failing. The code will fail if the call to foo comes
>> too early, specifically before the assignment happens, but maybe you can
>> post a full example that fails?
>>
>
> the call to foo is after the variable is initialised.
>
> I tested it by echoing gettype($x); both after initialising and inside
> the function
I'm not doubting your result, just saying that the result you got was
not form the code you posted.
> It was fine outside the function and then returned NULL inside it.
>
> I suspect the problem is in the way I am, 'eval' ing the code.
So there may be an issue somewhere in code you didn't post? I guess so!
<snip>
--
Ben.]]>Ben Bacarisse2014-02-23T20:59:00-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185009&th=123427#msg_185009
> On 23/02/14 18:59, Christoph Michael Becker wrote:
>> The Natural Philosopher wrote:
>>
>>> Consider
>>> <?php
>>> $x=array();
>>>
>>> function foo()
>>> {
>>> global $x;
>>> foreach($x as $p) // fails with invalid type
>>> {
>>> }
>>> }
>>> ?>
>>> ---------
>>>
>>> <?php
>>> $x=array();
>>> global $x;
>>> function foo()
>>> {
>>> global $x;
>>> foreach($x as $p) // works??
>>> {
>>> }
>>> }
>>> ?>
>>> ---------
>>> This behaviour seems only limited to arrays...
>>
>> No (unless there's a bug in a particular PHP version). Your first code
>> sample works fine without even a notice.
>>
>
> It doesn't here, but the code is being 'eval'ed' ...
>
Which is an important fact you conveniently forgot to add. The problem
is not in the code. The problem is in your design of the scripts.
First rule - eval() is nasty and should not be used.
Second rule - see first rule.
>> Using a global statement in the global scope is not wrong, but it's
>> useless in this case. See the PHP manual on global[1], especially the
>> note at the end of the section.
>
> In this case its the difference between the program working and not
> working.
>
It's the difference between a good design and a TNP design.
--
==================
Remove the "x" from my email address
Jerry Stuckle jstucklex@attglobal.net
==================]]>Jerry Stuckle2014-02-23T21:37:44-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185010&th=123427#msg_185010
> The Natural Philosopher <tnp@invalid.invalid> writes:
>
>> Consider
>> <?php
>> $x=array();
>>
>> function foo()
>> {
>> global $x;
>> foreach($x as $p) // fails with invalid type
>> {
>> }
>> }
>> ?>
>
> foo isn't called in this example, so what you've posted is not the
> actual code that's failing. The code will fail if the call to foo comes
> too early, specifically before the assignment happens, but maybe you can
> post a full example that fails?
>
> <snip>
>
Ben,
No surprise. TNP never tells the whole story until someone drags it out
of him - piece by piece. Then the story will change with each reply.
--
==================
Remove the "x" from my email address
Jerry Stuckle jstucklex@attglobal.net
==================]]>Jerry Stuckle2014-02-23T21:38:39-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185012&th=123427#msg_185012
> Consider <?php $x=array();
>
> function foo()
> {
> global $x;
> foreach($x as $p) // fails with invalid type
> {
> }
> }
> ?>
> ---------
>
> <?php $x=array();
> global $x;
> function foo()
> {
> global $x;
> foreach($x as $p) // works??
> {
> }
> }
> ?>
> ---------
> This behaviour seems only limited to arrays...
Which version of PHP? I copied your code almost literally and it works
flawlessly:
[mgogala@medo ~]$ cat /tmp/ttt.php
#!/usr/bin/php
<?php
$x=array(1,2,3);
function foo()
{
global $x;
foreach($x as $p) // fails with invalid type
{
print "$p\n";
}
}
foo();
?>
[mgogala@medo ~]$ /tmp/ttt.php
1
2
3
mgogala@medo ~]$ php -v
PHP 5.5.9 (cli) (built: Feb 18 2014 15:01:31)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
[mgogala@medo ~]$
It's Linux, F20.
--
Mladen Gogala
The Oracle Whisperer http://mgogala.byethost5.com]]>Mladen Gogala2014-02-23T22:09:06-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185332&th=123427#msg_185332
> On 23/02/14 18:59, Christoph Michael Becker wrote:
>> The Natural Philosopher wrote:
>>
>>> Consider
>>> <?php
>>> $x=array();
>>>
>>> function foo()
>>> {
>>> global $x;
>>> foreach($x as $p) // fails with invalid type
>>> {
>>> }
>>> }
>>> ?>
>>> ---------
>>>
>>> <?php
>>> $x=array();
>>> global $x;
>>> function foo()
>>> {
>>> global $x;
>>> foreach($x as $p) // works??
>>> {
>>> }
>>> }
>>> ?>
>>> ---------
>>> This behaviour seems only limited to arrays...
>>
>> No (unless there's a bug in a particular PHP version). Your first code
>> sample works fine without even a notice.
>>
>
> It doesn't here, but the code is being 'eval'ed' ...
That's one of the reasons, why it is NOT "nice" to store code in a
database and then use eval() to execute it.
--
Arno Welzel http://arnowelzel.de http://de-rec-fahrrad.de]]>Arno Welzel2014-03-19T15:26:54-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185338&th=123427#msg_185338
> Am 23.02.2014 21:35, schrieb The Natural Philosopher:
>> On 23/02/14 18:59, Christoph Michael Becker wrote:
>>> The Natural Philosopher wrote:
>>>
>>>> Consider
>>>> <?php
>>>> $x=array();
>>>>
>>>> function foo()
>>>> {
>>>> global $x;
>>>> foreach($x as $p) // fails with invalid type
>>>> {
>>>> }
>>>> }
>>>> ?>
>>>> ---------
>>>>
>>>> <?php
>>>> $x=array();
>>>> global $x;
>>>> function foo()
>>>> {
>>>> global $x;
>>>> foreach($x as $p) // works??
>>>> {
>>>> }
>>>> }
>>>> ?>
>>>> ---------
>>>> This behaviour seems only limited to arrays...
>>>
>>> No (unless there's a bug in a particular PHP version). Your first code
>>> sample works fine without even a notice.
>>>
>>
>> It doesn't here, but the code is being 'eval'ed' ...
>
> That's one of the reasons, why it is NOT "nice" to store code in a
> database and then use eval() to execute it.
>
>
>
actually it isn't.
It was because the global was essentially being declared in a
subroutine, which I had forgotten.
--
Ineptocracy
(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.]]>The Natural Philosoph2014-03-19T17:16:59-00:00Re: weird global issue
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185344&th=123427#msg_185344
> On 19/03/14 15:26, Arno Welzel wrote:
>> Am 23.02.2014 21:35, schrieb The Natural Philosopher:
>>> On 23/02/14 18:59, Christoph Michael Becker wrote:
>>>> The Natural Philosopher wrote:
>>>>
>>>> > Consider
>>>> > <?php
>>>> > $x=array();
>>>> >
>>>> > function foo()
>>>> > {
>>>> > global $x;
>>>> > foreach($x as $p) // fails with invalid type
>>>> > {
>>>> > }
>>>> > }
>>>> > ?>
>>>> > ---------
>>>> >
>>>> > <?php
>>>> > $x=array();
>>>> > global $x;
>>>> > function foo()
>>>> > {
>>>> > global $x;
>>>> > foreach($x as $p) // works??
>>>> > {
>>>> > }
>>>> > }
>>>> > ?>
>>>> > ---------
>>>> > This behaviour seems only limited to arrays...
>>>>
>>>> No (unless there's a bug in a particular PHP version). Your first code
>>>> sample works fine without even a notice.
>>>>
>>>
>>> It doesn't here, but the code is being 'eval'ed' ...
>>
>> That's one of the reasons, why it is NOT "nice" to store code in a
>> database and then use eval() to execute it.
>>
>>
>>
> actually it isn't.
>
> It was because the global was essentially being declared in a
> subroutine, which I had forgotten.
>
>
>
>