LiveWire Peer Support Network

Printable Version of Topic "How do I call upon variables set in a php function?"

- LiveWire Teen Forums & College Forums (http://www.golivewire.com)
-- (http://www.golivewire.com/forums/support-technical.html)
--- Programming & Application Development (http://www.golivewire.com/forums/forum-211-s-0.html)
---- How do I call upon variables set in a php function? (http://www.golivewire.com/forums/peer-yapbpop-support-a.html)


-- Posted by Mediocre at 3:42 pm on Aug. 21, 2008

edit: I needed to give it a global scope. /idiot

I intialize $valcookies as false. However, I then use my checkcookies() function, which, after checking, does:

$valcookies = true;

I tried to call $valcookies in another function after this had happened, and $valcookies was still false.


-- Posted by hI jAMES at 3:43 pm on Aug. 21, 2008

lol.. thanks for that.


-- Posted by qi at 3:47 pm on Aug. 21, 2008

Do a barrel roll!


-- Posted by allsmiles at 5:12 pm on Aug. 21, 2008

Can you show the source please? Sounds like you're checking if it's set as opposed to its value.


-- Posted by mcox05 at 11:28 pm on Aug. 22, 2008

Two big things to remember.
1. PHP and other web based languages are stateless. So if you are setting a variable to false or true then reloading and/or changing pages then the variable's value will be reset.

2. Are you sure that you are correctly using the scope of the variable.

EX 1:
public class X
{
//A field who's state will be retained (unless pages are reloaded or changed)
$cookVal;

public X()
{
$cookVal = true;
}

public checkVal()
{
return ($cookVal);
}
}

Calling checkVal() would return true.

EX 2:public class X
{
public X()
{
//local variable that will be garbage collected after method call is finished.
$cookVal = true;
}

public checkVal()
{
return ($cookVal);
}

}

Calling checkVal() would return false.


Forgive me if my PHP syntax is off. I haven't coded in it for a while but hopefully these two examples will clarify your problem. If you post your source code I can give you a more direct answer.


www.golivewire.com