Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #12890 > unrolled thread

weird global issue

Started byThe Natural Philosopher <tnp@invalid.invalid>
First post2014-02-23 18:33 +0000
Last post2014-02-23 22:09 +0000
Articles 14 — 8 participants

Back to article view | Back to comp.lang.php


Contents

  weird global issue The Natural Philosopher <tnp@invalid.invalid> - 2014-02-23 18:33 +0000
    Re: weird global issue richard <noreply@example.com> - 2014-02-23 13:40 -0500
    Re: weird global issue Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-23 19:59 +0100
      Re: weird global issue The Natural Philosopher <tnp@invalid.invalid> - 2014-02-23 20:35 +0000
        Re: weird global issue Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-23 16:37 -0500
        Re: weird global issue Arno Welzel <usenet@arnowelzel.de> - 2014-03-19 16:26 +0100
          Re: weird global issue The Natural Philosopher <tnp@invalid.invalid> - 2014-03-19 17:16 +0000
            Re: weird global issue Scott Johnson <noonehome@chalupasworld.com> - 2014-03-19 14:21 -0700
    Re: weird global issue Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-23 19:16 +0000
      Re: weird global issue The Natural Philosopher <tnp@invalid.invalid> - 2014-02-23 20:41 +0000
        Re: weird global issue Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-23 21:49 +0100
        Re: weird global issue Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-23 20:59 +0000
      Re: weird global issue Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-23 16:38 -0500
    Re: weird global issue Mladen Gogala <gogala.mladen@gmail.com> - 2014-02-23 22:09 +0000

#12890 — weird global issue

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2014-02-23 18:33 +0000
Subjectweird global issue
Message-ID<ledeug$a34$1@news.albasani.net>
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...

-- 
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.

[toc] | [next] | [standalone]


#12891

Fromrichard <noreply@example.com>
Date2014-02-23 13:40 -0500
Message-ID<m8zqif4pd47x.dcidve7f5rl3.dlg@40tude.net>
In reply to#12890
On Sun, 23 Feb 2014 18:33:52 +0000, 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...

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); }

[toc] | [prev] | [next] | [standalone]


#12892

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-02-23 19:59 +0100
Message-ID<530a450b$0$9514$9b4e6d93@newsspool1.arcor-online.net>
In reply to#12890
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.

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.

[1]
<http://www.php.net/language.variables.scope.php#language.variables.scope.global>

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#12898

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2014-02-23 20:35 +0000
Message-ID<ledm1l$omj$2@news.albasani.net>
In reply to#12892
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' ...

> 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.


>
> [1]
> <http://www.php.net/language.variables.scope.php#language.variables.scope.global>
>


-- 
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.

[toc] | [prev] | [next] | [standalone]


#12907

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-02-23 16:37 -0500
Message-ID<ledpn7$uor$2@dont-email.me>
In reply to#12898
On 2/23/2014 3:35 PM, The Natural Philosopher wrote:
> 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.

>
>>
>> [1]
>> <http://www.php.net/language.variables.scope.php#language.variables.scope.global>
>>
>>
>
>



-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#13215

FromArno Welzel <usenet@arnowelzel.de>
Date2014-03-19 16:26 +0100
Message-ID<5329B73E.1050404@arnowelzel.de>
In reply to#12898
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.



-- 
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de

[toc] | [prev] | [next] | [standalone]


#13220

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2014-03-19 17:16 +0000
Message-ID<lgcjeb$vjb$1@news.albasani.net>
In reply to#13215
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.




-- 
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.

[toc] | [prev] | [next] | [standalone]


#13225

FromScott Johnson <noonehome@chalupasworld.com>
Date2014-03-19 14:21 -0700
Message-ID<lgd1rp$mgk$1@dont-email.me>
In reply to#13220
On 3/19/14, 10:16 AM, The Natural Philosopher wrote:
> 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.
>
>
>
>

I hate when I do that...so much lost time.

Scotty

[toc] | [prev] | [next] | [standalone]


#12893

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-02-23 19:16 +0000
Message-ID<0.2fc68c45e0c0e5aba897.20140223191649GMT.87k3cltza6.fsf@bsb.me.uk>
In reply to#12890
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.

[toc] | [prev] | [next] | [standalone]


#12901

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2014-02-23 20:41 +0000
Message-ID<ledmed$ps4$1@news.albasani.net>
In reply to#12893
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.

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.

[toc] | [prev] | [next] | [standalone]


#12902

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-02-23 21:49 +0100
Message-ID<530a5eef$0$9525$9b4e6d93@newsspool1.arcor-online.net>
In reply to#12901
The Natural Philosopher wrote:

> 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

[toc] | [prev] | [next] | [standalone]


#12904

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-02-23 20:59 +0000
Message-ID<0.89ed91ca399623476111.20140223205900GMT.8761o5tujv.fsf@bsb.me.uk>
In reply to#12901
The Natural Philosopher <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.

[toc] | [prev] | [next] | [standalone]


#12908

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-02-23 16:38 -0500
Message-ID<ledpou$uor$3@dont-email.me>
In reply to#12893
On 2/23/2014 2:16 PM, 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?
>
> <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
==================

[toc] | [prev] | [next] | [standalone]


#12910

FromMladen Gogala <gogala.mladen@gmail.com>
Date2014-02-23 22:09 +0000
Message-ID<pan.2014.02.23.22.09.06@gmail.com>
In reply to#12890
On Sun, 23 Feb 2014 18:33:52 +0000, 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...

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web