Re: [PHP] Variable scope
On Jul 14, 2011, at 3:54 PM, Karl DeSaulniers wrote: Can anyone explain this to me. function sendEmail($uname,$subjField,$firstname,$lastname,$email, $reply,$e_cc,$e_bcc,$comments,$ip,$Date,$time){ $uname = trim($uname); $subjField = trim($subjField); $firstname = trim($firstname); $lastname = trim($lastname); $email = trim($email); $reply = trim($reply); $e_cc = trim($e_cc); $e_bcc = trim($e_bcc); $comments = trim($comments); $ip = trim($ip); $Date = trim($Date); $time = trim($time); //If I trace here email, reply and the CCs are ok if(($firstname && strlen($firstname = trim($firstname)) > 2) && ($lastname && strlen($lastname = trim($lastname)) > 2)) { $fullname = $firstname." ".$lastname; } else { $fullname = "Member"; } $fullname = trim($fullname); $To = ""; $from = ""; $headerTXT = ""; $bounce_email = CO_NAME." <".BOUNCE_ADDR.">"; $subject = $subjectField; $bulk = false; //What kind of email is being sent //Email exists, no Cc or Bcc if(!empty($email) && empty($_email_cc) && empty($_email_bcc)) { $To = $fullname." <".$email.">"; $from = "Member <".$reply.">"; $headerTXT = "New message from ".CO_NAME." member ".$uname; $bulk = false; } //Email empty, Cc exists no Bcc else if(empty($email) && !empty($e_cc) && empty($e_bcc)) { $To = $bounce_email; $from = "Member <".$reply.">"; $headerTXT = "New message from ".CO_NAME." member ".$uname; $bulk = true; } ... //If I trace here $To, $from have everything except the "<(anything between)>", so for instance.. $To = John Doe ; $from = Member ; Have you looked at the output in the page source? The <> may be getting eaten by the browser. You may want to use htmlentities on your trace output (but not the actual variables). not $To = John Doe ; $from = Member ; So $email and $reply are loosing their value/scope But the ".CO_NAME." and ".BOUNCE_ADDR." are working!?!? I assume these are defined constants, which don't have scope (or, perhaps more accurately, always have global scope). This is also in a page with many other email functions just like this one and they work. Only difference is the $To and $from are not inside an if() { statement. How did my variables loose scope??? One other note, if I put.. $To = htmlspecialchars($fullname." <".$email.">"); then $To is correct when I trace.. $To = John Doe ; but it wont send because the smtp mail will not send to $To = John DoeI MUST be doing something wrong. Also, I did read about using Name but the other email functions work fine with those, so I'm not sure what's going on. TIA, Best, Karl DeSaulniers Design Drumm http://designdrumm.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Variable scope
Can anyone explain this to me. function sendEmail($uname,$subjField,$firstname,$lastname,$email, $reply,$e_cc,$e_bcc,$comments,$ip,$Date,$time){ $uname = trim($uname); $subjField = trim($subjField); $firstname = trim($firstname); $lastname = trim($lastname); $email = trim($email); $reply = trim($reply); $e_cc = trim($e_cc); $e_bcc = trim($e_bcc); $comments = trim($comments); $ip = trim($ip); $Date = trim($Date); $time = trim($time); //If I trace here email, reply and the CCs are ok if(($firstname && strlen($firstname = trim($firstname)) > 2) && ($lastname && strlen($lastname = trim($lastname)) > 2)) { $fullname = $firstname." ".$lastname; } else { $fullname = "Member"; } $fullname = trim($fullname); $To = ""; $from = ""; $headerTXT = ""; $bounce_email = CO_NAME." <".BOUNCE_ADDR.">"; $subject = $subjectField; $bulk = false; //What kind of email is being sent //Email exists, no Cc or Bcc if(!empty($email) && empty($_email_cc) && empty($_email_bcc)) { $To = $fullname." <".$email.">"; $from = "Member <".$reply.">"; $headerTXT = "New message from ".CO_NAME." member ".$uname; $bulk = false; } //Email empty, Cc exists no Bcc else if(empty($email) && !empty($e_cc) && empty($e_bcc)) { $To = $bounce_email; $from = "Member <".$reply.">"; $headerTXT = "New message from ".CO_NAME." member ".$uname; $bulk = true; } ... //If I trace here $To, $from have everything except the "<(anything between)>", so for instance.. $To = John Doe ; $from = Member ; not $To = John Doe ; $from = Member ; So $email and $reply are loosing their value/scope But the ".CO_NAME." and ".BOUNCE_ADDR." are working!?!? This is also in a page with many other email functions just like this one and they work. Only difference is the $To and $from are not inside an if() { statement. How did my variables loose scope??? One other note, if I put.. $To = htmlspecialchars($fullname." <".$email.">"); then $To is correct when I trace.. $To = John Doe ; but it wont send because the smtp mail will not send to $To = John DoeI MUST be doing something wrong. Also, I did read about using Name but the other email functions work fine with those, so I'm not sure what's going on. TIA, Best, Karl DeSaulniers Design Drumm http://designdrumm.com
[PHP] Variable Scope with require_once
Hello, all. I have a working app that runs in PHP 4.4.9 on the server. I develop the app on my local environment, which runs PHP 5.2.6. The setup is thus: From my index, I have require_once('config.php'); require_once('dbstuff.php'); config.php looks like this: dbstuff.php looks like this: $dbh = mysql_connect($db_host, $db_user, $db_password) or die('Could not connect: ' . mysql_error()); } ...snipped... ?> For some reason, the $db_host, $db_user, and so on defined in config.php are not accessible from dbstuff.php on my development environment. I wlll get [timestamp] [error] [client 127.0.0.1] PHP Notice: Undefined variable: db_host in somepath\db_stuff.php on line whatever which causes 'Could not connect: some MySQL error message that is obtained when attempting to login with a nonexistent user' This works on the production environment (the one with PHP version 4.4.9). register_globals is Off in both environments. What is going on, here? Regards, A. Wilson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] SOLVED Re: [PHP] Variable Scope problem passing function parameters
Jim McIntyre wrote: metastable wrote: Jim McIntyre wrote: $phpMail = new PHPMailer(); $phpMail->From = $from; $phpMail->AddAddress($this->to); $phpMail->Subject = $subject; $phpMail->Body = $body; return $phpMail->Send(); Never mind - I found the problem. It was a lowly typo. Ugh. -Jim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable Scope problem passing function parameters
metastable wrote: Jim McIntyre wrote: $phpMail = new PHPMailer(); $phpMail->From = $from; $phpMail->AddAddress($this->to); $phpMail->Subject = $subject; $phpMail->Body = $body; return $phpMail->Send(); $this -> to it has no meaning in the scope of your class. Apparently, neither do the other variable names. If I replace that with a string containing my email address, it sends an email... but everything else in the email is blank - no body, no subject, etc. How do I reference the arguments passed to the function when setting properties or calling methods of the instance of PHPMailer? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable Scope problem passing function parameters
Jim McIntyre wrote: > I'm new to PHP 5 and classes, but I've done a lot of ActionScript. > > I'm trying to use PHPMailer inside my own class (a service for > AMFPHP). I'm having problems getting the data that'spassed into my > class's send() method to the instance of the PHPMailer. > > Basically, I have this: > > class EmailAMF { > public function send ($from, $to, $subject, $body) { > require('class.phpmailer.php'); > > $phpMail = new PHPMailer(); > $phpMail->From = $from; > $phpMail->AddAddress($this->to); > $phpMail->Subject = $subject; > $phpMail->Body = $body; > return $phpMail->Send(); > > } > } > > As far as I can tell, it sends an essentially blank email to nobody > (but returns true. > > If I replace any of my function variables with strings, like this: > >$phpMail->AddAddress('m...@example.com'); > > the email actually "gets" that data. > > What am I missing? > > Thanks, > Jim > $this -> to it has no meaning in the scope of your class. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Variable Scope problem passing function parameters
I'm new to PHP 5 and classes, but I've done a lot of ActionScript. I'm trying to use PHPMailer inside my own class (a service for AMFPHP). I'm having problems getting the data that'spassed into my class's send() method to the instance of the PHPMailer. Basically, I have this: class EmailAMF { public function send ($from, $to, $subject, $body) { require('class.phpmailer.php'); $phpMail = new PHPMailer(); $phpMail->From = $from; $phpMail->AddAddress($this->to); $phpMail->Subject = $subject; $phpMail->Body = $body; return $phpMail->Send(); } } As far as I can tell, it sends an essentially blank email to nobody (but returns true. If I replace any of my function variables with strings, like this: $phpMail->AddAddress('m...@example.com'); the email actually "gets" that data. What am I missing? Thanks, Jim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable Scope from child to parent
Ok, I forgot to reply-all to the list. Here we go again. On Wed, May 14, 2008 at 6:33 PM, Tyson Vanover <[EMAIL PROTECTED]> wrote: > I am trying to get a child class to pass an array of valid keys to it's > parent when the constructor is run, and the parent appends the data to one >> of it's array of valid keys. Then it walks through an array pulling out > values that have valid keys, and putting them in an array for processing > later. I have tried explicitly stating the variable scope. > > abstract class parentclass > { > protected $vkeys= array('title1','title2','title3'); > protected $a; > > function __construct($set = NULL, $special = NULL) > { >self::$this->vkeys= array_merge(self::$this->vkeys, $special); >foreach($set as $key=>$value) >{ > if (in_array($key,self::$this->vkeys)) > { >$this->a[$key] = $value; > } >} >print_r(self::$this->vkeys); //output below >print_r(self::$this->a); //output below > } > } > > class childclass extends parentclass > { > protected $vkeys= array('titleA', 'titleB', 'TitleC'); > > function __construct($set, $special = NULL) > { >parent::__construct($set, self::$this->vkeys); >unset(self::$this->vkeys); > } > } > > Unfortunately it seems to duplicate the child's array instead of appending. > Explicitly stating scope does not seem to help. > > > print_r(self::$this->vkeys); > Array ( > [0] => titleA > [1] => titleB > [2] => titleB > [3] => titleA > [4] => titleB > [5] => titleB > ) > > print_r(self::$this->a); > Array() > > Any thoughts? A couple. First, as Gabriel said, replace self::$this-> with just $this->. Second, the protected variable $vkeys in the childclass effectively overwrites the variable of the same name in the parent class, so it already has the values you are trying to merge (array('titleA', 'titleB', 'TitleC')) when the childclass is instantiated before you even call the constructor. That's why the constructor duplicates the values. Try this for the constructor for your childclass: function __construct($set, $special = array()) { $merged = array_merge($special, array('titleA', 'titleB', 'TitleC')); parent::__construct($set, $merged); } Andrew > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable Scope from child to parent
tyson i never saw self::$this->vkeys doing this you actually are callig the var inside $this->vkeys do self::$vkeys as static or $this->vkeys as object don't mix regards On Wed, May 14, 2008 at 7:33 PM, Tyson Vanover <[EMAIL PROTECTED]> wrote: > I am trying to get a child class to pass an array of valid keys to it's > parent when the constructor is run, and the parent appends the data to one > of it's array of valid keys. Then it walks through an array pulling out > values that have valid keys, and putting them in an array for processing > later. I have tried explicitly stating the variable scope. > > abstract class parentclass > { > protected $vkeys= array('title1','title2','title3'); > protected $a; > > function __construct($set = NULL, $special = NULL) > { >self::$this->vkeys= array_merge(self::$this->vkeys, $special); >foreach($set as $key=>$value) >{ > if (in_array($key,self::$this->vkeys)) > { >$this->a[$key] = $value; > } >} >print_r(self::$this->vkeys); //output below >print_r(self::$this->a); //output below > } > } > > class childclass extends parentclass > { > protected $vkeys= array('titleA', 'titleB', 'TitleC'); > > function __construct($set, $special = NULL) > { >parent::__construct($set, self::$this->vkeys); >unset(self::$this->vkeys); > } > } > > Unfortunately it seems to duplicate the child's array instead of appending. > Explicitly stating scope does not seem to help. > > > print_r(self::$this->vkeys); > Array ( > [0] => titleA > [1] => titleB > [2] => titleB > [3] => titleA > [4] => titleB > [5] => titleB > ) > > print_r(self::$this->a); > Array() > > Any thoughts? > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Los sabios buscan la sabiduría; los necios creen haberla encontrado. Gabriel Sosa -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Variable Scope from child to parent
I am trying to get a child class to pass an array of valid keys to it's parent when the constructor is run, and the parent appends the data to one of it's array of valid keys. Then it walks through an array pulling out values that have valid keys, and putting them in an array for processing later. I have tried explicitly stating the variable scope. abstract class parentclass { protected $vkeys= array('title1','title2','title3'); protected $a; function __construct($set = NULL, $special = NULL) { self::$this->vkeys= array_merge(self::$this->vkeys, $special); foreach($set as $key=>$value) { if (in_array($key,self::$this->vkeys)) { $this->a[$key] = $value; } } print_r(self::$this->vkeys); //output below print_r(self::$this->a); //output below } } class childclass extends parentclass { protected $vkeys= array('titleA', 'titleB', 'TitleC'); function __construct($set, $special = NULL) { parent::__construct($set, self::$this->vkeys); unset(self::$this->vkeys); } } Unfortunately it seems to duplicate the child's array instead of appending. Explicitly stating scope does not seem to help. print_r(self::$this->vkeys); Array ( [0] => titleA [1] => titleB [2] => titleB [3] => titleA [4] => titleB [5] => titleB ) print_r(self::$this->a); Array() Any thoughts? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] variable scope in nested functions
Is there a way to access a variable defined in a function from a nested function, without putting the variable in the global scope? Perhaps some code will make my question clearer: function enclosing() { $variable1; function enclosed() { can I access $variable1 from here? } } Thanks, fbv -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable scope
On Thu, 14 Mar 2002, George Nicolae wrote: >> > > > function a($var_a) >> > { >> > b(); >> > } >> > >> > function b() >> > { >> > global $var_a; >> > echo $var_a; >> > } >> > >> > a("hello word!"); >> > ?> >> > >> > why function b() don't echo anything? >> >> Because $var_a has not, at any point, been defined in the global scope. > > how i declare $var_a as global? Define it somewhere within the global context (i.e., outside of any function. Or do what you have done, refer to it in function b() with the "global" keyword. But you can do that all day long and that won't make it local to function a() unless you modify that function as well. miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable scope
On Friday 15 March 2002 00:34, George Nicolae wrote: > "Jason Wong" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED]... > > > On Thursday 14 March 2002 21:53, George Nicolae wrote: > > > I have the following code: > > > > > > > > function a($var_a) > > > { > > > b(); > > > } > > > > > > function b() > > > { > > > global $var_a; > > > echo $var_a; > > > } > > > > > > a("hello word!"); > > > ?> > > > > > > why function b() don't echo anything? > > > > Because $var_a has not, at any point, been defined in the global scope. > > how i declare $var_a as global? Strictly speaking, in php you don't. Any variable declared outside of a function is what can be loosely called "global". To access such variables inside a function you must tell PHP you want to use the global version of that variable. $doo = 'dah'; $foo = 'bar'; a(); function a() { global $doo; echo $doo; # displays 'dah'; echo $fool # displays nothing. } In your code above, you haven't defined $var_a anywhere outside of a function. Thus it is a local variable that only exists inside function a(). That is why function b() doesn't see it. > > > can I resolve this problem without > > > calling b($var_a)? > > > > I'm curious as to what you're trying to achieve. Could you enlighten me? > > I have a big(=many lines) function "a()" and a little one "b()". I call > function "b()" from function "a()" and alsow from many other php files. I > need to midify the function "b()" without modify any other php files. > That's why I don't want to use b($var_a). The easiest solution I can see right now is to explicitly define $var_a. -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* The use of anthropomorphic terminology when dealing with computing systems is a symptom of professional immaturity. -- Edsger Dijkstra */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable scope
"Jason Wong" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED]... > On Thursday 14 March 2002 21:53, George Nicolae wrote: > > I have the following code: > > > > > function a($var_a) > > { > > b(); > > } > > > > function b() > > { > > global $var_a; > > echo $var_a; > > } > > > > a("hello word!"); > > ?> > > > > why function b() don't echo anything? > > Because $var_a has not, at any point, been defined in the global scope. how i declare $var_a as global? > > can I resolve this problem without > > calling b($var_a)? > > I'm curious as to what you're trying to achieve. Could you enlighten me? I have a big(=many lines) function "a()" and a little one "b()". I call function "b()" from function "a()" and alsow from many other php files. I need to midify the function "b()" without modify any other php files. That's why I don't want to use b($var_a). > -- > Jason Wong -> Gremlins Associates -> www.gremlins.com.hk > Open Source Software Systems Integrators > * Web Design & Hosting * Internet & Intranet Applications Development * > > /* > "I will make no bargains with terrorist hardware." > -- Peter da Silva > */ Best regards, George Nicolae IT Manager ___ PaginiWeb.com - Professional Web Design www.PaginiWeb.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Variable scope
On Thursday 14 March 2002 21:53, George Nicolae wrote: > I have the following code: > > function a($var_a) > { > b(); > } > > function b() > { > global $var_a; > echo $var_a; > } > > a("hello word!"); > ?> > > why function b() don't echo anything? Because $var_a has not, at any point, been defined in the global scope. > can I resolve this problem without > calling b($var_a)? I'm curious as to what you're trying to achieve. Could you enlighten me? -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* "I will make no bargains with terrorist hardware." -- Peter da Silva */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Variable scope
I have the following code: why function b() don't echo anything? can I resolve this problem without calling b($var_a)? Best regards, George Nicolae IT Manager ___ PaginiWeb.com - Professional Web Design www.PaginiWeb.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] variable scope
Something most have just been wigging out or something because I can't get it to do it again. Maybe I just did something wrong, but it seems to be working now. Thanks for the help though, Dave "Lars Torben Wilson" <[EMAIL PROTECTED]> wrote in message 1015973063.2146.82.camel@ali">news:1015973063.2146.82.camel@ali... > On Tue, 2002-03-12 at 14:38, David Johansen wrote: > > I have a little chunk of code that checks to see if a variable exists and if > > not then it sets it. It goes like this: > > > > if (empty($page)) > > { > >$page = "login"; > > } > > > > I then end that part of the php script after doing what I need to. Then I > > start up again on the same html page and I assumed that $page had a value > > assigned to it because of the above code, but it appears that it doesn't. Is > > this supposed to happen or is something weird happening. I added that little > > bit of code to the second chunk of code and it fixed the problem. Is that > > the "right" way to do it? Is that variable only available in the > > that it's declared in? Thanks, > > Dave > > Shouldn't be. A variable is valid from the point it's declared until > it's unset(), or goes out of scope (the function within which it was > declared ends), or the script ends. > > Can you provide a short script which exhibits this behaviour? > > > -- > Torben Wilson <[EMAIL PROTECTED]> > http://www.thebuttlesschaps.com > http://www.hybrid17.com > http://www.inflatableeye.com > +1.604.709.0506 > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] variable scope
On Tue, 2002-03-12 at 14:38, David Johansen wrote: > I have a little chunk of code that checks to see if a variable exists and if > not then it sets it. It goes like this: > > if (empty($page)) > { >$page = "login"; > } > > I then end that part of the php script after doing what I need to. Then I > start up again on the same html page and I assumed that $page had a value > assigned to it because of the above code, but it appears that it doesn't. Is > this supposed to happen or is something weird happening. I added that little > bit of code to the second chunk of code and it fixed the problem. Is that > the "right" way to do it? Is that variable only available in the > that it's declared in? Thanks, > Dave Shouldn't be. A variable is valid from the point it's declared until it's unset(), or goes out of scope (the function within which it was declared ends), or the script ends. Can you provide a short script which exhibits this behaviour? -- Torben Wilson <[EMAIL PROTECTED]> http://www.thebuttlesschaps.com http://www.hybrid17.com http://www.inflatableeye.com +1.604.709.0506 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] variable scope
I have a little chunk of code that checks to see if a variable exists and if not then it sets it. It goes like this: if (empty($page)) { $page = "login"; } I then end that part of the php script after doing what I need to. Then I start up again on the same html page and I assumed that $page had a value assigned to it because of the above code, but it appears that it doesn't. Is this supposed to happen or is something weird happening. I added that little bit of code to the second chunk of code and it fixed the problem. Is that the "right" way to do it? Is that variable only available in the that it's declared in? Thanks, Dave -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] variable scope / preg_replace_callback question
function _compile_lang($key){ global $_lang; return $_lang[$key[1]]; } // End _compile_lang HTH Regards, Andrey Hristov - Original Message - From: "Peter Bowyer" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Monday, December 03, 2001 3:45 PM Subject: [PHP] variable scope / preg_replace_callback question > Hi, > > Does anyone know of a way of passing other variables to the function being called by >preg_replace_callback? For instance, I have the following code: > > function smarty_compile_lang($tpl_source) { > // en.php contains a very large array, $_lang > include_once '/home/test/en.php'; > global $_lang; > return preg_replace_callback('/##(.+?)##/', '_compile_lang', $tpl_source); > } > > function _compile_lang($key){ > // If I include $_lang here then it gets > // loaded again and again - bad for performance as it is _very_ large > return $_lang[$key[1]]; > } // End _compile_lang > > Somehow I need to make $_lang global, or pass it through the function >preg_replace_callback as an arguement for _compile_lang() - which I don't think is possible? > > What can I do? I've tried a global statement everywhere I can think of... and I >don't want to include en.php outside the functions, as it'll be included then whether the templates are being compiled or not. I'm > guessing that I may want to look at a higher level of abstraction from these >functions, as even if I have the include in the current place, it means that every time a template is compiled it gets reloaded (though that's > not as bad as every time preg_replace_callback() finds something) whereas the best >way would be to load it once per script invocation - and then only when the templates wer being compiled... > > Suggestions? > > TIA, > Peter. > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] variable scope / preg_replace_callback question
Hi, Does anyone know of a way of passing other variables to the function being called by preg_replace_callback? For instance, I have the following code: function smarty_compile_lang($tpl_source) { // en.php contains a very large array, $_lang include_once '/home/test/en.php'; global $_lang; return preg_replace_callback('/##(.+?)##/', '_compile_lang', $tpl_source); } function _compile_lang($key){ // If I include $_lang here then it gets // loaded again and again - bad for performance as it is _very_ large return $_lang[$key[1]]; } // End _compile_lang Somehow I need to make $_lang global, or pass it through the function preg_replace_callback as an arguement for _compile_lang() - which I don't think is possible? What can I do? I've tried a global statement everywhere I can think of... and I don't want to include en.php outside the functions, as it'll be included then whether the templates are being compiled or not. I'm guessing that I may want to look at a higher level of abstraction from these functions, as even if I have the include in the current place, it means that every time a template is compiled it gets reloaded (though that's not as bad as every time preg_replace_callback() finds something) whereas the best way would be to load it once per script invocation - and then only when the templates wer being compiled... Suggestions? TIA, Peter. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Variable scope problem
> -Original Message- > From: Joe Nelson [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, March 28, 2001 6:17 PM > To: php list > Subject: [PHP] Variable scope problem > > > I have two functions like the ones below. If the first one creates a > variable as global, shouldn't it be accesible to the second function? > > function setGlobal() { > global $test; > > $test = "123"; > } > > function getGlobal() { > global $test; > > echo $test; > } Global doesn't CREATE global variables, it allows variables to be used as globals. For example, in the above setup, you would do this instead: function setGlobal() { global $test; $test = "123"; return $test; } $test = setGlobal(); function getGlobal() { global $test; echo $test; } And now, getGlobal will echo $test as: 123 Of course, if you are looking to create global variables in a single function, then you simply would do something like this: function setGlobal() { global $test; $test[0] = "123"; $test[1] = "456"; $test[2] = "789"; return $test; } and that would return the array $test, and therefore: $test = setGlobal(); $test would be an array, and then you could set global in any function $test and be able to use multiple values (as per the array). This is easier than globaling 20 or so values you may need. Jason Lotito www.NewbieNetwork.net Where those who can, teach; and those who can, learn. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Variable scope problem
I have two functions like the ones below. If the first one creates a variable as global, shouldn't it be accesible to the second function? function setGlobal() { global $test; $test = "123"; } function getGlobal() { global $test; echo $test; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Variable scope
-Original Message- From: Martin Skjoldebrand [mailto:[EMAIL PROTECTED]] Sent: Saturday, March 10, 2001 3:22 PM To: [EMAIL PROTECTED] Subject: [PHP] Variable scope > How do I make a variable persistent between webpages? Take a look at the session functions described at: <http://php.net/manual/en/ref.session.php> -- Christian Jorgensen http://www.razor.dk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Variable scope
How do I make a variable persistent between webpages? V e r b a l -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] variable scope -- unsetting vars inside functions
When a function finishes execution does it automatically clean up its local variables or should I manually unset() them? For example: function do_something() { $local_var = 'something really big...'; return true; } Does $local_var immediately vanish from existence when do_something finishes executing? Thanks! -- Michael A. Smith <[EMAIL PROTECTED]> Director of Data Systems, wcities.com ICQ: 35884415 :wq -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Variable Scope
I find the VB way (or VBScript anyway) of doing functions backwards. My recollection is that if I call a function blah... x = blahfunction('cow') 'blahfunction' itself has to be defined *in* the function to get the value back. I can't even think of a good example, it's so foreign to me and I haven't done much new vbscript in a while. To get 'x' to equal something, you'd have to have a line in the blahfunction that actually says blahfunction='moo' *That's* weird to me. Anyway, not meant to be bashing, but you brought up VB. What's helped me to think of globals in PHP is that I know variables defined in the main area of the page work on that level. As I go deeper into the code (down a level) into a function, if I need something from a level above, I have to say GLOBAL $varname to make sure I get the varname I want. It's helped me immensely over the last year, but mostly by making me do functions with argument lists and return values, rather than modifying GLOBAL variables. My functions are far more portable between projects than they were a few years ago. "Karl J. Stubsjoen" wrote: > I'm a bit troubled with the fact that variables declared at the top of your > script are not then global to functions within that script. That in order > to see that variable within a function you'd have to declare it as global in > the function (understanding that the variable could have been set at the top > of the script). This seems extremely backwards to me (me being a VB guy). > > Would someone kindly shine the light down upon me with an explanation of > this logic? And why can't I possibly declare the variable as Global at the > top of my script? > > Thats a big "H" -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Variable Scope
How about allowing for metaPasses within the castle? Am sure this topic came up, what's the conclusion? Philip On Thu, 18 Jan 2001, Rasmus Lerdorf wrote: > > I'm a bit troubled with the fact that variables declared at the top of your > > script are not then global to functions within that script. That in order > > to see that variable within a function you'd have to declare it as global in > > the function (understanding that the variable could have been set at the top > > of the script). This seems extremely backwards to me (me being a VB guy). > > > > Would someone kindly shine the light down upon me with an explanation of > > this logic? And why can't I possibly declare the variable as Global at the > > top of my script? > > Once upon a time in a cold land far far away, a brave programming team was > building a castle. The castle was large and complex with countless rooms > and secret passageways. On one frightful day the team got stuck. They > had built one too many rooms with one too many secret passageways and > suddenly the castle was beginning to crumble. A desperate plea went out > to the local wizard who was strong and powerful. The local wizard looked > at the problem, saw the many rooms and the many secret passageways and > gave the following decree: "Thou must post a sign in each and every room > declaring which secret passageways connect to this room." The brave > programming team feared the wizard and implemented the decree immediately > although they really didn't understand how it would help. What they found > was that a number of secret passageways did not connect to anything and > others connected to places that didn't make any sense. After fixing these > the castle was solid again. > > The moral of this tale is that one must declare ones' global variables > that one intends to use within a function. In languages like C where this > is not required one can end up with all sorts of weird side effects when a > function changes a variable that the programmer intended to be local to > the function and it affects code elsewhere in the system. This is almost > impossible to track down. The tale of the brave programming team led to > this being a requirement in PHP. > > -Rasmus > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Variable Scope
> I'm a bit troubled with the fact that variables declared at the top of your > script are not then global to functions within that script. That in order > to see that variable within a function you'd have to declare it as global in > the function (understanding that the variable could have been set at the top > of the script). This seems extremely backwards to me (me being a VB guy). > > Would someone kindly shine the light down upon me with an explanation of > this logic? And why can't I possibly declare the variable as Global at the > top of my script? Once upon a time in a cold land far far away, a brave programming team was building a castle. The castle was large and complex with countless rooms and secret passageways. On one frightful day the team got stuck. They had built one too many rooms with one too many secret passageways and suddenly the castle was beginning to crumble. A desperate plea went out to the local wizard who was strong and powerful. The local wizard looked at the problem, saw the many rooms and the many secret passageways and gave the following decree: "Thou must post a sign in each and every room declaring which secret passageways connect to this room." The brave programming team feared the wizard and implemented the decree immediately although they really didn't understand how it would help. What they found was that a number of secret passageways did not connect to anything and others connected to places that didn't make any sense. After fixing these the castle was solid again. The moral of this tale is that one must declare ones' global variables that one intends to use within a function. In languages like C where this is not required one can end up with all sorts of weird side effects when a function changes a variable that the programmer intended to be local to the function and it affects code elsewhere in the system. This is almost impossible to track down. The tale of the brave programming team led to this being a requirement in PHP. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Variable Scope
A nice explanation, via the mind of Lars Torben Wilson, exists here : http://marc.theaimsgroup.com/?l=php-general&m=97717398101040&w=2 Also, you may want to check out : http://www.php.net/manual/en/language.constants.php http://www.php.net/manual/en/function.define.php Philip On Thu, 18 Jan 2001, Karl J. Stubsjoen wrote: > I'm a bit troubled with the fact that variables declared at the top of your > script are not then global to functions within that script. That in order > to see that variable within a function you'd have to declare it as global in > the function (understanding that the variable could have been set at the top > of the script). This seems extremely backwards to me (me being a VB guy). > > Would someone kindly shine the light down upon me with an explanation of > this logic? And why can't I possibly declare the variable as Global at the > top of my script? > > Thats a big "H" > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Variable Scope
I'm a bit troubled with the fact that variables declared at the top of your script are not then global to functions within that script. That in order to see that variable within a function you'd have to declare it as global in the function (understanding that the variable could have been set at the top of the script). This seems extremely backwards to me (me being a VB guy). Would someone kindly shine the light down upon me with an explanation of this logic? And why can't I possibly declare the variable as Global at the top of my script? Thats a big "H" -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]