Re: subroutines?

From: Date: Thu, 09 Aug 2001 17:16:27 +0000
Subject: Re: subroutines?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-62020@lists.php.net to get a copy of this message
<billfarr@ages.com> wrote: > As a new PHP convert, I ran into this same problem (below) yesterday while > designing a form. ASP programmers (if they're THINKING) will use subs to > keep "if" blocks tidy. In ASP, I'd use something like: > > If Request.ServerVariables( "REQUEST_METHOD" ) = "GET" Then > DisplayForm > Else > SendFormData > End If > > which at first I replaced with: > if ( getenv( "REQUEST_METHOD" ) == "GET" ) { > display_form(); > } else { > send_email(); > } > ...making functions out of what used to be subroutines. All well-and-good, > except my global variables disappeared and nothing I could do (even > declaring the few important ones explicitly global) seemed to allow me to > get at the variable contents. Bill, You need to declare the variables global within each function that access them or access the variable via the $GLOBALS array. $var = 'something'; function go() { return $GLOBALS["var"]; } or function go() { global $var; return $var; } echo go(); -- Steve Werby President, Befriend Internet Services LLC http://www.befriend.com/

« previous php.general (#62020) next »