PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue May 23, 2017 3:50 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Apr 26, 2017 9:06 am 
Offline
Forum Commoner

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 83
Q.
On this page:
https://www.tutorialspoint.com/php/php_ ... _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. :)


Top
 Profile  
 
PostPosted: Wed Apr 26, 2017 10:04 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6418
Location: WA, USA
1. https://3v4l.org/7FOcP

http://floating-point-gui.de/
Read everything that site has and come back if you still have questions.

2.
UniqueIdeaMan wrote:
I can understand the following is considered TRUE because the value of the string is not blank or empty and holds a value.

Forget "blank". Every string is true except for "" and "0".

UniqueIdeaMan wrote:
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";

Forget "blank". If the array has anything in it at all then it is true. Only empty arrays are false.

UniqueIdeaMan wrote:
I guess if the value held "0" then it would be FALSE too. Correct ? Correct me if I'm wrong.

No.

UniqueIdeaMan wrote:
My REAL question is this following one. How can that variable be considered FALSE since the NULL holds a NULL ?

Null is not a value. Null is the absence of a value. This is more prominent in SQL but the same principle holds in virtually every programming language out there.
Syntax: [ Download ] [ Hide ]
$false_null = NULL;

This creates a variable. It looks like null is the value, but stop thinking about it as a value. The variable has no value.

Because PHP is so loosely typed, null will act like the default value for every type when treated as such. Null as a string is an empty string, null as a number is 0, and null as a boolean is false.

UniqueIdeaMan wrote:
A negative "-" holds a negative "-". Or a "zero" holding a "zero" is TRUE.

I have no idea what you're talking about.

UniqueIdeaMan wrote:
Right ?

Doubtful.

UniqueIdeaMan wrote:
$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 ?

No. Don't make this more complicated than it has to be: null == false.

UniqueIdeaMan wrote:
Unless, I'm understanding NULL wrong some-how.

Completely.

UniqueIdeaMan wrote:
I see when we create tables in mysql, we assign whether a column should be NULL or not.

No. What you are doing is saying whether a column is able to not have a value. Again, because null is the absence of a value.

UniqueIdeaMan wrote:
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.

"assign it not to be NULLable", correct.

UniqueIdeaMan wrote:
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.

"If it's set to NULLable", correct.

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

With integers, yes. Unlike floating-point numbers, integers are exact.

UniqueIdeaMan wrote:
I'm having trouble understand "It evaluates to FALSE in a Boolean context" for reasons mentioned just above.

Syntax: [ Download ] [ Hide ]
if ($variable) {

That is a boolean context: PHP needs to decide whether it should do the if code or not, and so must come to a true or false conclusion. Whatever you do in that condition will eventually be turned into a boolean value, even if PHP has to do it automatically for you.

UniqueIdeaMan wrote:
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 ?

I don't know how you aren't understanding that. Surely a single, trivial example is sufficient:
Syntax: [ Download ] [ Hide ]
$variable = null;
if (isset($variable)) {
        echo "true";
} else {
        echo "false"; // <--
}


Top
 Profile  
 
PostPosted: Wed Apr 26, 2017 3:37 pm 
Offline
Forum Commoner

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 83
Thanks Requinix.
Shame we can't give REPUTATIONS or THANK YOUs in this forum, like some others.


Top
 Profile  
 
PostPosted: Wed Apr 26, 2017 3:45 pm 
Offline
Forum Commoner

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 83
One question. Would this be valid variable:

$float_var = 100.10;

If so, then would this be a valid boolean:

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


Top
 Profile  
 
PostPosted: Thu Apr 27, 2017 2:51 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6418
Location: WA, USA
UniqueIdeaMan wrote:
Would this be valid variable:

$float_var = 100.10;

I don't see anything invalid in that code.

UniqueIdeaMan wrote:
If so, then would this be a valid boolean:

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

Ignoring the misplaced semicolon,

That code says "perform the assignment of 100.1 to the variable $float_var, have the expression evaluate to the value 100.1, and convert that value to a boolean".
Code:
if ((bool)100.1)

If you did == then it would be "perform a loose comparison between the variable $float_var and the value 100.1, and have the expression evaluate to whether the two are equal". === would be the same but with a strict comparison.

UniqueIdeaMan wrote:
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".

No, the advice is still correct. Did you check that 3v4l link I posted earlier?


Top
 Profile  
 
PostPosted: Fri Apr 28, 2017 4:19 pm 
Offline
Forum Commoner

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 83
Requinix,

I actually missed your first lin and noticed the 2nd and checked that one out.
Now, I noticed your first link:
https://3v4l.org/7FOcP

What is it ? An experiment page where we play with php codes to see how they output ?


Top
 Profile  
 
PostPosted: Sat Apr 29, 2017 2:13 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6418
Location: WA, USA
I wouldn't say you use it to "play with" PHP, but it is a nice place to demonstrate how code works in multiple versions of PHP.

The point I was demonstrating was that $value should be 0.0 but it evaluates to true.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Exabot [Bot] and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group