[thelist] help me understand global variable in PHP

sasha spam at bittersweet2.com
Thu Jan 2 12:33:00 CST 2003


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

> I'm having some trouble getting a variable value in my page. My code
> uses some global variables which I don't really understand. Here is
> what happens:
> ---------------------------------------
> First, the script declares the variable:
> $prodname = "";
> ---------------------------------------
>
> Then, I am parsing XML, which uses 3 user defined functions called
> by:
> ---------------------------------------
> xml_set_element_handler($xml_parser, "startElement", "endElement");
> xml_set_character_data_handler($xml_parser, "characterData");
> ---------------------------------------
> (its event driven, every time the parser hits a handler, it runs the
> appropriate function, same for character data)
>
> Within the three functions I have the following line:
> ---------------------------------------
> global $prodname (with some other vars)
> ---------------------------------------
>
> Then, at the end of the endElement() function (the last one to deal
> with the data) the variable is again set to blank
> ---------------------------------------
> $prodname = "";
> } (end function)
> ---------------------------------------
>
> Now, my problem is - I want to pass along $prodname to the next page
> I am going to via the query string. So I have something like
> /path.php?p=$prodname. This variable however is always empty. This
> line of code is OUTSIDE the functions of course.
>
> Now, I assume this is part of the problem, $prodname gets set to
> blank so there is no value. So I tried, inside the function doing
>
> $prod = $prodname;
>
> so it wouldn't get reset..this still doesn't work.
>
> You can see the full page code at:
> www.silverheaven.com/reg/code.html.
>
> If you look at the code, I am having a similar problem with
> $message..
>
> 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

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?

--
sasha



More information about the thelist mailing list