0

Q.
On this page:
https://www.tutorialspoint.com/php/php_variable_types.htm

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 Reverend Jim: Removed BBCode - this site uses MarkDown syntax

5
Contributors
9
Replies
41
Views
3 Months
Discussion Span
Last Post by Reverend Jim
0

Booleans are integral values. Floating point values (including doubles) are not. Comparing one to the other results in NAN (not a number) result. Time for you to refresh your math.

0

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.

Have you tried to evaluate it through var_dump()?

var_dump(empty("\t"), empty(" "), empty(""));

Edited by cereal

0

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!";

0

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 UI

0

rprofitt,

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 ?

https://www.tutorialspoint.com/php/php_variable_types.htm

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

0

When a tutorials writes don't do this, I don't.

If you want to ask why they wrote that, see if the author left a way for you to contact them.

I see you've struggled with data types in many posts. I think you mixed up assignment (the single equal sign), the confuser to many about TRUE and FALSE and when to use the triple equal sign. Keep going, you'll get it done.

0

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.

3

The advice was given as

Don't use double as Booleans.

Your question was

Why can't you use a double as a boolean ?

You weren't told that you couldn't. You were told, in effect, that you shouldn't. There's a big difference. You shouldn't run with scissors and you shouldn't use GOTOs, but you can if you want to (except for the GOTOs).

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.