Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Boolean's Double Troubles

boolean double

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 uniqueideaman

uniqueideaman

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts

Posted 26 April 2017 - 06:10 AM

Hi,
 
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). :D
 
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. :)

Edited by uniqueideaman, 26 April 2017 - 06:16 AM.


#2 uniqueideaman

uniqueideaman

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts

Posted 26 April 2017 - 12:26 PM

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 &lt; 16.5);echo "You're under age!";else echo "You're old enough!";


#3 uniqueideaman

uniqueideaman

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts

Posted 26 April 2017 - 12:50 PM

 

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".

Edited by uniqueideaman, 26 April 2017 - 12:57 PM.


#4 uniqueideaman

uniqueideaman

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts

Posted 28 April 2017 - 01:32 PM

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.

Edited by uniqueideaman, 28 April 2017 - 01:34 PM.