dsaba Posted December 1, 2007 Share Posted December 1, 2007 <?php class testClass{ var $lala = 'no'; function samba() { if ($this->lala == 'no') { $temp = $this->lion(); echo "temp is: $temp<br>"; if ($temp == 'no') { echo 'this is not right'; } } } function lion() { if ($this->lala == 'no') { return 0; } } } $obj = new testClass(); $whatever = $obj->samba(); ?> on this line: echo "temp is: $temp<br>"; if ($temp == 'no') { echo 'this is not right'; } it will echo out temp as '0', yet it this condition will check to be true $temp == 'no', even though temp is obviously not 'no' but really '0'... what's going on? How can I fix it? (to better understand you can run the code) -thanks Quote Link to comment Share on other sites More sharing options...
wsantos Posted December 1, 2007 Share Posted December 1, 2007 because of this line function lion() { if ($this->lala == 'no') { return 0; // <--- This is why } Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 1, 2007 Share Posted December 1, 2007 Nevermind >.> (erased post) Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 1, 2007 Author Share Posted December 1, 2007 yes, but the integer 0 is not equal to the string 'no' yet the condition still evaluates to true!! this is the problem, and I don't think it should be happening. Talk about this. i'm running this in PHP 5 btw Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 1, 2007 Author Share Posted December 1, 2007 here's the answer: when making comparisons with integers and strings, PHP will convert the string into an integer, and this converted string will always equal 0 (correct me if i'm wrong) so when comparing the integer 0 to the string 'no' it is really comparing: the integer 0 to the translated integer representation of the string (which is 0) of 'no' this yielding the condition of 0 == 'no' to TRUE *edit OR I could use the === operator to check for comparison within the same type so 'no' === 0 will evaluate as FALSE Quote Link to comment 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.