Gossamer Forum
Home : General : Chit Chat :

Is it me?

Quote Reply
Is it me?
Ok...can i just see what people generally think about this. In PHP, if a variable is defined within a function (PHPH's equivelant of a sub) you HAVE TO define it as a global before you can use it in other subs! Is it me, or is this a really stupid idea? I would much prefer to just define the local variables within the function, as with Perls 'my' statement. Anyone else got any thoughts on this?

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Is it me? In reply to
If you are programming properly using strict you have to do the same. Either specifying globals, my or local

Last edited by:

PaulW: Nov 19, 2001, 1:53 PM
Quote Reply
Re: [AndyNewby] Is it me? In reply to
I thought this approach was no longer necessary in the latest release of PHP? Or is that the development release? This used to be the case with Perl too, until they changed this to be optional. You should still specify your variables however. It's good practice.

#!/usr/bin/perl -W

use strict;

- wil
Quote Reply
Re: [AndyNewby] Is it me? In reply to
If you place all the input in an array, you can just make that global and it saves writing a whole list...

Cheers,
Michael Bray
Quote Reply
Re: [Wil] Is it me? In reply to
The stupid thing though is that the following code would not pass the variable $test as a global into a sub calling within a sub!

Code:
<?php

test1();
test2();

function test1() {

$test = "testing";

global $testing;

}

function test2() {

echo $testing;

}

Anyone know why this is? As I'm sure you can tell, I'm still quite a newbie to PHP Tongue

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Is it me? In reply to
Well it encourages better programming otherwise you will have variables flying all over the place.

You'll get an error trying it with strict....
Code:
use strict;

print one() + two();

sub one {

$num 5;
return $num;

}

sub two {

return $num;

}

Thats a crap example but hey Unsure

For that, you'd need to use my $num (twice) or use vars qw/$num/; (global)

Last edited by:

PaulW: Nov 20, 2001, 5:53 AM