play_ Posted July 29, 2009 Share Posted July 29, 2009 When form is submitted: $error = false; $error .= ( ! $validation->username($username) ) ? false : '<li >Invalid username. Usernames can contain letters, numbers, - and _</li>'; The method to validate username( just returns false if it's empty) function username($username) { //TODO: regexp if( empty($username) ) { return false; } else { return true; } } So, according to this, is $username is empty, the validation method should return false. which is a bool. if( ! $error ) { // First of all, code in here is executing. It shouldn't because $error should be false. // Second, the line below returns 'string' instead of bool. why? is it because of .= ? echo gettype($error); Link to comment https://forums.phpfreaks.com/topic/167918-solved-variable-should-be-bool-but-its-string/ Share on other sites More sharing options...
corbin Posted July 29, 2009 Share Posted July 29, 2009 $error .= Perhaps you meant =? Link to comment https://forums.phpfreaks.com/topic/167918-solved-variable-should-be-bool-but-its-string/#findComment-885678 Share on other sites More sharing options...
play_ Posted July 29, 2009 Author Share Posted July 29, 2009 No, i need it to be .= to accumulate the error message(s) Link to comment https://forums.phpfreaks.com/topic/167918-solved-variable-should-be-bool-but-its-string/#findComment-885679 Share on other sites More sharing options...
corbin Posted July 29, 2009 Share Posted July 29, 2009 Well when ever you use .= the thing on the right side is casted to a string... And false casted to a string is just an empty string. So pretty much you'll have to not use .= there. If you're setting it to either false or an error there, what does it matter? Or do you have other things? You could use an array then use count() and implode() to see if there are errors and if so smack them together or something. Or you could have an error flag that would contain the bool then keep the actual errors in a different variable. Link to comment https://forums.phpfreaks.com/topic/167918-solved-variable-should-be-bool-but-its-string/#findComment-885681 Share on other sites More sharing options...
play_ Posted July 29, 2009 Author Share Posted July 29, 2009 Well when ever you use .= the thing on the right side is casted to a string... And false casted to a string is just an empty string. So pretty much you'll have to not use .= there. If you're setting it to either false or an error there, what does it matter? Or do you have other things? You could use an array then use count() and implode() to see if there are errors and if so smack them together or something. Or you could have an error flag that would contain the bool then keep the actual errors in a different variable. Right. I am trying to keep it simple. what i'll do is i'll use if( ! empty($error) ) { instead of if( ! $error ) { because as you said, 'And false casted to a string is just an empty string.'. thanks. Link to comment https://forums.phpfreaks.com/topic/167918-solved-variable-should-be-bool-but-its-string/#findComment-885683 Share on other sites More sharing options...
corbin Posted July 29, 2009 Share Posted July 29, 2009 Ooo didn't think about that ;p. Link to comment https://forums.phpfreaks.com/topic/167918-solved-variable-should-be-bool-but-its-string/#findComment-885684 Share on other sites More sharing options...
play_ Posted July 29, 2009 Author Share Posted July 29, 2009 However, $error starts as false (bool), not string. so .= converts a bool to string, apparently $a = false; $a .= false; echo gettype($a); // returns string weird. but kind of makes sense, because what would $a .= true return if .= didnt convert the var to string? it'd be like dividing by 0 and the universe would implode. Link to comment https://forums.phpfreaks.com/topic/167918-solved-variable-should-be-bool-but-its-string/#findComment-885685 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.