[thelist] help me understand global variable in PHP

sasha spam at bittersweet2.com
Thu Jan 2 13:13:01 CST 2003


On Thu, 2 Jan 2003 10:58:58 -0800 (PST), Tom Dell'Aringa
<pixelmech at yahoo.com> wrote:

> --- sasha <spam at bittersweet2.com> wrote:
>> Variables within functions don't effect variables within the global
>> scope
>> (which is what you seem to want), unless you tell them to.
>>
>> function yourfunction() {
>> 	do stuff..
>> 	return $desired_variable;
>> 	-or -
>> 	return array($desired_variable1, $desired_variable2);
>> }
>>
>> $product = yourfunction();
>> - or -
>> $prod_array = yourfunction();
>> $desired_variable1 = $prod_array[0];
>> $desired_variable2 = $prod_array[1];
>>
>> Clear as mud?
>
> Makes sense I think, but I can't call these functions to a variable
> that I need, they are being called by event handlers (the php xml
> parsing stuff). How can I get my $prodname variable available to me
> with the right value outside that function without doing what you
> stated above...or can I? (I'm screwed if I can't..).
>
> For reference, my code again is at:
>
> www.silverheaven.com/reg/code.html
>
> Tom
>
>
> =====
> var me = tom.pixelmech.webDeveloper();
>
> http://www.pixelmech.com/
> http://www.maccaws.com/
> [Making A Commercial Case for Adopting Web Standards]
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
> http://mailplus.yahoo.com

yourfunction() {
	do stuff..
	// when extracted, $var1 will be named after its key 'var1'.
	return array(var1 => $var1, var2 => $var2);
}

on event {
	extract(yourfunction());
}

Make sure you read up on extract first to handle variable name colisions.
http://www.php.net/manual/en/function.extract.php

--
sasha



More information about the thelist mailing list