Jump to content

Setting a Variable to False vs Empty Value


Recommended Posts

The technical difference is that false and empty string are two different data types. One is boolean and the other is a string.

 

While an empty string becomes false when converted to boolean, it is not itself false, it is a string with zero characters in it.

 

When initializing variables, I will use true or false if my variable is meant to be a boolean and I'll use an empty string if I intend for the variable to be a string. In other programming languages you would not be allowed to set a string to false or a boolean to "". I personally advise against changing the type of data in your variable.

Link to post
Share on other sites

Many thanks davej, justsomeguy. and Ingolme.

 

The difference is now clear as well as when to use each. The difference between setting the variable to NULL and setting it to an empty string is also clear theoretically, however, I am comfortable with using the empty string but I would love to see one example of how to sett the variable to a NULL value but not to an empty string.

Link to post
Share on other sites

I meant one example of when would I use $a = NULL; instead of $a = '';

 

I don't think your question entirely makes sense. Variables are initialized if the code needs them to be initialized, to a value that makes sense for that code, but not all variables need to be initialized. It all depends on how the code is written and how the variable is used.

Link to post
Share on other sites

$a = null;

...

if (/*some condition*/) {
  // set $a to a string value if some condition was true, the empty string might be a valid value here
}

...

if ($a === null) {
  // the above condition was not true and the variable was not changed from the default, so do something else
}
If you initially set $a to an empty string then you wouldn't be able to test it to determine if the value changed. That code assumes that an empty string would be a valid value to set it inside the one if statement.

 

If you want another example of why it's bad to use values like that to indicate no value, look at examples for strpos. It returns the index of a substring inside a string. If the string starts with the substring, then it returns 0 which indicates that the substring starts at position 0. It returns false if it wasn't found. So if you only check if (!substr($str, $substr)) then if it returns 0 saying that it starts at the beginning your code will fail because 0 will evaluate to false. You need to explicitly check for false to see if it wasn't found in that case, not just a loose value check.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...