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 Link to comment https://forums.phpfreaks.com/topic/79748-solved-inside-php-class-variable-logical-operator-not-working-why/ 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 } Link to comment https://forums.phpfreaks.com/topic/79748-solved-inside-php-class-variable-logical-operator-not-working-why/#findComment-403864 Share on other sites More sharing options...
pocobueno1388 Posted December 1, 2007 Share Posted December 1, 2007 Nevermind >.> (erased post) Link to comment https://forums.phpfreaks.com/topic/79748-solved-inside-php-class-variable-logical-operator-not-working-why/#findComment-403869 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 Link to comment https://forums.phpfreaks.com/topic/79748-solved-inside-php-class-variable-logical-operator-not-working-why/#findComment-403870 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 Link to comment https://forums.phpfreaks.com/topic/79748-solved-inside-php-class-variable-logical-operator-not-working-why/#findComment-403884 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.