Boolean's Double Troubles

Q.
On this page:

On section “Interpreting other types as Booleans”, it says:

“Don’t use double as Booleans”.

I do not understand this. Why can’t you use a double as a boolean ?
Why can’t we use the following example ?

EXAMPLE 1:

$int_var = 100.10;

If($int_var != 100.10);
echo “FALSE”;
else echo “TRUE”;

EXAMPLE 2:

$int_var = 16.5;

If($int_var < 16.5);
echo “You’re under age!”;
else echo “You’re old enough!”;

Q2.
On the same page under the same section as above, it says:

"Each of the following variables has the truth value embedded in its name when it is used in a Boolean context.

$true_num = 3 + 0.14159;
$true_str = “Tried and true”
$true_array[49] = “An array element”;
$false_array = array();
$false_null = NULL;
$false_num = 999 - 999;
$false_str = “”; "

Now my question …

I can understand the following is considered TRUE because the value of the number is not zero.
$true_num = 3 + 0.14159;

I can understand the following is considered TRUE because the value of the string is not blank or empty and holds a value.
"$true_str = “Tried and true”
Saying all this, I guess white space or the TAB/INDENT is considered as FALSE if some-how there can be “values” set to them one way or the other. But, I guess not.

I can understand the following is considered TRUE because the value of the ARRAY is not zero or blank/empty. Correct ? (I have a little doubt about this one).
$true_array[49] = “An array element”;

I can understand the following is considered FALSE because the value of the ARRAY is blank/empty. I guess if the value held “0” then it would be FALSE too. Correct ? Correct me if I’m wrong.
$false_array = array();

My REAL question is this following one. How can that variable be considered FALSE since the NULL holds a NULL ? A negative “-” holds a negative “-”. Or a “zero” holding a “zero” is TRUE. Right ? Don’t understand this logic.
$false_null = NULL;
Let’s face it. Imagine you’re a robot who accepts donations. Now, I come along and program you to not accept any donations anymore. No holding money in your pockets anymore. Some guy comes and tries giving you money but you refuse. When I get back, I try seeing if your pockets are empty (NULL) and find it is. Did you live-up to the NULL ? Yes, you did. And so, it is a TRUE NULL. Not FALSE. Right ?
Unless, I’m understanding NULL wrong some-how. I see when we create tables in mysql, we assign whether a column should be NULL or not. I’m guessing, if we assign it not to be NULL then on every input to the table that column must get inputted a value. Else, mysql will spit an error. If it’s set to NULL then that means the column can be not filled even though other columns are getting filled in a go. Correct, or have I got my wires crossed some-how ?

I can understand the following is false because the value of the variable would be zero. Right ?
$false_num = 999 - 999;

That page further says on the NULL section:
“A variable that has been assigned NULL has the following properties −”

It evaluates to FALSE in a Boolean context.
“It returns FALSE when tested with IsSet() function”.

I’m having trouble understand “It evaluates to FALSE in a Boolean context” for reasons mentioned just above.
:confused:
Maybe, some examples from you guys can clear this misconception on my part (if any). :smiley:

“It returns FALSE when tested with IsSet() function”.
Anyone kind enough to show 2 examples of the IsSet() function here where the variable with a NULL value would be considered FALSE ?
:confused:

Thank you for your time and help. :slight_smile:

because floating point representation/math is imprecise.

Mmm. How about 2 examples ?

I cringed at the first statement since it is not an integer but a float. Very confusing!

Also the loose comparison operates != will no doubt cause problems.

I would recommend a more up to date tutorial and as mentioned in a previous post to use the latest PHP7 declare( strict_types = 1 );

1 Like

The following 2 examples were not from any tutorial. I made them up. Seems like I made a mistake mixing INT and FLOAT.

$int_var = 100.10;

If($int_var != 100.10);echo “FALSE”;else echo “TRUE”;

EXAMPLE 2:

$int_var = 16.5;

If($int_var < 16.5);echo “You’re under age!”;else echo “You’re old enough!”;

One question. Would this be valid variable:

$float_var = 100.10;

If so, then would this be a valid boolean:

$float_var = 100.10;

If($float_var = 100.10);
echo “TRUE”;
else echo “FALSE”;

Here, I used a double/float as boolean and if it works then the following is bad advice, is it not ?
“Don’t use double as Booleans”.

A Boolean variable only has two possible values either true or false.

PHP is not exact and everything which is not zero or NULL is considered true.

1 Like

All sorts of errors in that bit of code - single = on the first line means assignment and not comparison, semi-colon on the end of the first line ends things earlier than you expect. Those aside, the if() clause is not really using the variable itself as a Boolean, it’s using the result of comparing it with a value as a Boolean. The result of a comparison is generally a Boolean, either the comparison is true or it is not.

Stuff like

$int_var = 100.10;

isn’t exactly wrong as such, it’s just incredibly confusing for either someone else trying to modify or repair your code, or for you in five years time when you’ve forgotten what it does. The whole point of starting a variable name with something to suggest what might be in the variable is that it’s actually correct, so you wouldn’t use $int_ to start a float, or $flt_ to start an integer. You could, but you should not. Read up on ‘Hungarian Notation’, I think that’s what this is called.

You can see the confusion on another thread (or maybe earlier up this one) where someone was using $mysqli as the variable name for their PDO connection, which led to people correcting the syntax of stuff because they thought it was a mysqli connection.

4 Likes

Yep! My code had erros someone pointed out on another source saying it should have looked like this:

If($float_var == 100.10) {
echo “TRUE”;}
else {echo “FALSE”;}

But is the above a valid boolean ? If so, then why should we not use doubles as booleans like the link says ?

Look at the Boolean section. It says not to use double as boolean.

Because 1.5 may not actually be 1.5, so the boolean can fail when for all intents and purposes they are the same. It could be 1.499995 or it could be 1.5444444 when you’re looking for 1.5.

The only way a non-integer numeric value should even be considered to be used as a boolean comparison is if you round it to an agreed upon number of decimal places.

<?php// these are essentially 100.10
$val1 = 100.1001;
$val2 = 100.0950;

if ($val1 == $val2)  {
    echo "TRUE";
} else {
   echo "FALSE";
}
echo "<br>";
if (round($val1, 2) == round($val2, 2))  {
    echo "TRUE";
} else {
   echo "FALSE";
}
5 Likes

Well done! Well spoken! No one has done it better than this explanation or example. Everything is now clear to me. Have noted your code sample on my notepad++ note. :winky:

Now, do you mind checking out my other threads. I need help in one of them on the mysqli prepared statements. I need a code sample from you (like you gave here) for me to learn from it.

There are literally hundreds of threads here with prepared statements in them. Surely you can find one you can relate to :shifty:

1 Like

Everything that has different value then 0 or NULL evaluates to TRUE.

Why NULL evaluates to FALSE?
NULL means there is nothing in that variable, it is created but no value is assigned, thats null.
So everything that is empty (even empty string) evaluates to false.

You can look at comparison tables to get more detailed answer about this.

But, which one you deem relevant to my case in that thread of mine ?

I was told this:

2.5 may not actually be 2.5, so the boolean can fail when for all intents and purposes they are the same. It could be 2.499995 or it could be 2.5444444 when you’re looking for 2.5.
The only way a non-integer numeric value should even be considered to be used as a boolean comparison is if I round it to an agreed upon number of decimal places.

<?php// these are essentially 900.10
$val1 = 900.1001;
$val2 = 900.0950;

if ($val1 == $val2) {
echo "TRUE";
} else {
echo "FALSE";
}
echo "<br>";
if (round($val1, 2) == round($val2, 2)) {
echo "TRUE";
} else {
echo "FALSE";
}

This thread can now be closed.

:):):slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.