rockinaway Posted October 5, 2007 Share Posted October 5, 2007 var $testvar; function check_test() { if (!isset($this->testvar) || $this->testvar = '') echo 'Nothing set!'; } function test() { $this->testvar = 'Test'; return $this->testvar; } } When I try this mt check_test() function always returns the error for the variable being empty or not present, eventhough I have it set in the function test() when it is called. Even if I put test() before check_test() the same thing happens.. what could be the problem? Quote Link to comment https://forums.phpfreaks.com/topic/71984-solved-function-variables-s/ Share on other sites More sharing options...
emehrkay Posted October 5, 2007 Share Posted October 5, 2007 $this->testvar = '' will always be true you mean == or even better === Quote Link to comment https://forums.phpfreaks.com/topic/71984-solved-function-variables-s/#findComment-362601 Share on other sites More sharing options...
rockinaway Posted October 5, 2007 Author Share Posted October 5, 2007 Sorry my mistake.. missed ! New code.. still problems.. explained below var $testvar; function test() { $this->testvar = 'Test'; return $this->testvar; } } function check_test() { if (!isset($this->testvar) || $this->testvar != '') echo 'Nothing set!'.$this->testvar; } This the new code.. but I found a problem. Eventhough $testvar is a global variable.. as you can see it is set in test()... however if I try retrieving in it check_test() nothing is returned.. If I do the following: function check_test() { $this->testvar = 'sdasd'; if (!isset($this->testvar) || $this->testvar != '') echo 'Nothing set!'.$this->testvar; } Then it returns the value when it is in it's own function.. but still not the values set in other functions.. Why is this? Quote Link to comment https://forums.phpfreaks.com/topic/71984-solved-function-variables-s/#findComment-362620 Share on other sites More sharing options...
emehrkay Posted October 5, 2007 Share Posted October 5, 2007 if (!isset($this->testvar) || $this->testvar != '') this says if testvar isnt set or testvar is not eqal to '' <-- those are opposite Quote Link to comment https://forums.phpfreaks.com/topic/71984-solved-function-variables-s/#findComment-362625 Share on other sites More sharing options...
rockinaway Posted October 5, 2007 Author Share Posted October 5, 2007 Now trying: (!isset($this->testvar)) Still doesn't return the variable as I mentioned above.. Quote Link to comment https://forums.phpfreaks.com/topic/71984-solved-function-variables-s/#findComment-362639 Share on other sites More sharing options...
emehrkay Posted October 5, 2007 Share Posted October 5, 2007 just try echo $this->testvar; or you could initially define it as false and just do if(!$this->testvar) whatever Quote Link to comment https://forums.phpfreaks.com/topic/71984-solved-function-variables-s/#findComment-362643 Share on other sites More sharing options...
rockinaway Posted October 5, 2007 Author Share Posted October 5, 2007 It still doesn't get the value set in another function though... Quote Link to comment https://forums.phpfreaks.com/topic/71984-solved-function-variables-s/#findComment-362762 Share on other sites More sharing options...
rockinaway Posted October 5, 2007 Author Share Posted October 5, 2007 Fixed Quote Link to comment https://forums.phpfreaks.com/topic/71984-solved-function-variables-s/#findComment-362775 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.