Re: <% init

From: Date: Tue, 24 Oct 2000 23:37:12 +0000
Subject: Re: <% init
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-21819@lists.php.net to get a copy of this message
Thanks Theodor! I can see that I need to do some studying on how PHP handles variables. Teodor Cimpoesu wrote: > Hi Tim! > On Sun, 22 Oct 2000, Tim Sawyer wrote: > > > Not that simple. I'm setting a cookie and it won't work in a function. > > Here's > > a code snip; > > > > 1 <?php > > 2 //Do the cookie thing. > > 3 if ($go) { > > 4 //write the cookies if Go button pressed, expires 90 days > > 5 setcookie("selvar[company]", $company, time() +7776000, > > "/"); > > 6 setcookie("selvar[location]", $location, time() +7776000, > > "/"); > > 7 } else { > > 8 //Initialize vars from cookie if not > > 9 $company=$selvar[company]; > > 10 $location=$selvar[location]; > > 11 } > > 12 ... > > 39 ?> > > 40 <html> > > 41 <head> > > 42 <title>... > > > I am convinced in my recommandation :-P, let's see: > > <%init()%> > <html> > <% > // > // page stuff > // > %> > </html> > <% > function init() > { > global $HTTP_GET_VARS,$HTTP_COOKIE_VARS; > $GP = &$HTTP_GET_VARS; > $GK = &$HTTP_COOKIE_VARS; //shorthands > > if ($GP['go']) { > //write the cookies if Go button pressed, expires 90 days > setcookie("selvar[company]", $GP['company'], time() +7776000, > "/"); > setcookie("selvar[location]", $GP['location'], time() +7776000, > "/"); > } else { > //Initialize vars from cookie if not > $GP['company']=$GK['selvar']['company']; > $GP['location']=$GK['selvar']['location']; > } > } > %> > > I rewrote your function with the asumption of using track_vars > on',register > globals off', which is `Generally Recommended'[tm]. > > if you still like the lazy-programmer-way :^) of having all the vars at > hand (w/o knowing where are they coming from though), you can do an > extract($GLOBALS) right at the start of the function, to import all the vars > into your local scope. > > -- teodor > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net -- Tim Sawyer :wq

« previous php.general (#21819) next »