Jump to content

[SOLVED] variable should be bool but its string


play_

Recommended Posts

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);

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.

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.

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.