j.silver Posted December 7, 2016 Share Posted December 7, 2016 Dear all; Is there any technical difference between initially setting a variable to false or setting it to an empty value at the beginning of a script, i.e., $x = false; vs $x=''; Link to comment Share on other sites More sharing options...
davej Posted December 8, 2016 Share Posted December 8, 2016 You initially set a variable to something reasonable for its intended use. False is a boolean while '' is an empty string. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 8, 2016 Share Posted December 8, 2016 It's common to use null if you want to give something a value which indicates that it has no value. false and the empty string are both discrete values. null is a special value that means that it has no value. Link to comment Share on other sites More sharing options...
Ingolme Posted December 8, 2016 Share Posted December 8, 2016 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 comment Share on other sites More sharing options...
j.silver Posted December 8, 2016 Author Share Posted December 8, 2016 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 comment Share on other sites More sharing options...
Ingolme Posted December 8, 2016 Share Posted December 8, 2016 $a = null; Link to comment Share on other sites More sharing options...
j.silver Posted December 8, 2016 Author Share Posted December 8, 2016 I meant one example of when would I use $a = NULL; instead of $a = ''; Link to comment Share on other sites More sharing options...
Ingolme Posted December 8, 2016 Share Posted December 8, 2016 Usually I use null if the variable is meant to contain an object but I don't have the object yet. Link to comment Share on other sites More sharing options...
davej Posted December 8, 2016 Share Posted December 8, 2016 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 comment Share on other sites More sharing options...
justsomeguy Posted December 8, 2016 Share Posted December 8, 2016 $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 comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now