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

Home » Imported messages » comp.lang.php » weird global issue
Show: Today's Messages :: Unread Messages :: Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
Message by The Natural Philosoph is ignored  [reveal message]  [reveal all messages by The Natural Philosoph]  [stop ignoring this user] Go to next message
Re: weird global issue [message #184993 is a reply to message #184992] Sun, 23 February 2014 13:40 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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); }
Message by Christoph Michael Bec is ignored  [reveal message]  [reveal all messages by Christoph Michael Bec]  [stop ignoring this user] Go to previous messageGo to next message
Re: weird global issue [message #184995 is a reply to message #184992] Sun, 23 February 2014 14:16 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
add to buddy list
ignore all messages by this user
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.
Message by The Natural Philosoph is ignored  [reveal message]  [reveal all messages by The Natural Philosoph]  [stop ignoring this user] Go to previous messageGo to next message
Message by The Natural Philosoph is ignored  [reveal message]  [reveal all messages by The Natural Philosoph]  [stop ignoring this user] Go to previous messageGo to next message
Message by Christoph Michael Bec is ignored  [reveal message]  [reveal all messages by Christoph Michael Bec]  [stop ignoring this user] Go to previous messageGo to next message
Re: weird global issue [message #185006 is a reply to message #185003] Sun, 23 February 2014 15:59 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
add to buddy list
ignore all messages by this user
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 16:37 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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 16:38 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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 17:09 Go to previous messageGo to next message
Mladen Gogala is currently offline  Mladen Gogala
Messages: 13
Registered: December 2010
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
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 11:26 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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
Message by The Natural Philosoph is ignored  [reveal message]  [reveal all messages by The Natural Philosoph]  [stop ignoring this user] Go to previous messageGo to next message
Re: weird global issue [message #185344 is a reply to message #185338] Wed, 19 March 2014 17:21 Go to previous message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
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: Sun Apr 20 16:18:23 EDT 2025

Total time taken to generate the page: 7.22991 seconds