FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » weird global issue
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
weird global issue [message #184992] Sun, 23 February 2014 18:33 Go to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
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.
Re: weird global issue [message #184993 is a reply to message #184992] Sun, 23 February 2014 18:40 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
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); }
Re: weird global issue [message #184994 is a reply to message #184992] Sun, 23 February 2014 18:59 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
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.gl obal>

--
Christoph M. Becker
Re: weird global issue [message #184995 is a reply to message #184992] Sun, 23 February 2014 19:16 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
The Natural Philosopher <tnp(at)invalid(dot)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.
Re: weird global issue [message #185000 is a reply to message #184994] Sun, 23 February 2014 20:35 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
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.gl obal>
>


--
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.
Re: weird global issue [message #185003 is a reply to message #184995] Sun, 23 February 2014 20:41 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 23/02/14 19:16, Ben Bacarisse wrote:
> The Natural Philosopher <tnp(at)invalid(dot)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.
Re: weird global issue [message #185004 is a reply to message #185003] Sun, 23 February 2014 20:49 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
The Natural Philosopher wrote:

> On 23/02/14 19:16, Ben Bacarisse wrote:
>> The Natural Philosopher <tnp(at)invalid(dot)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
Re: weird global issue [message #185006 is a reply to message #185003] Sun, 23 February 2014 20:59 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
The Natural Philosopher <tnp(at)invalid(dot)invalid> writes:

> On 23/02/14 19:16, Ben Bacarisse wrote:
>> The Natural Philosopher <tnp(at)invalid(dot)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.
Re: weird global issue [message #185009 is a reply to message #185000] Sun, 23 February 2014 21:37 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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.gl obal>
>>
>>
>
>



--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: weird global issue [message #185010 is a reply to message #184995] Sun, 23 February 2014 21:38 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/23/2014 2:16 PM, Ben Bacarisse wrote:
> The Natural Philosopher <tnp(at)invalid(dot)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(at)attglobal(dot)net
==================
Re: weird global issue [message #185012 is a reply to message #184992] Sun, 23 February 2014 22:09 Go to previous messageGo to next message
Mladen Gogala is currently offline  Mladen Gogala
Messages: 13
Registered: December 2010
Karma: 0
Junior Member
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
Re: weird global issue [message #185332 is a reply to message #185000] Wed, 19 March 2014 15:26 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
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
Re: weird global issue [message #185338 is a reply to message #185332] Wed, 19 March 2014 17:16 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
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.
Re: weird global issue [message #185344 is a reply to message #185338] Wed, 19 March 2014 21:21 Go to previous message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: string length
Next Topic: Job offer is hereby withdrawn - it's done!
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 21 13:27:32 GMT 2024

Total time taken to generate the page: 0.02868 seconds