Why are PHP booleans both lower case and upper case? - language-design

Is there a difference between true and TRUE or false and FALSE in PHP?

If you intend to use JSON then the standard RFC7159 says :
The literal names MUST be lowercase. No other literal names are allowed.
And from Php 5.6 :
json_decode() now rejects non-lowercase variants of the JSON literals true, false and null at all times, as per the JSON specification
And according to PSR-2 standard :
PHP keywords MUST be in lower case.
The PHP constants true, false, and null MUST be in lower case.
Ps.: I could not post link to the RFC7159 because of SO limitations.

Constants are case-sensitive per default. But for symmetry to the other identifier namespaces, they can be defined case-insensitively:
define("mixedCASE", 123, TRUE);
print MiXeDcAsE;
And that's just how TRUE and FALSE were pre-declared. (They aren't parser/language builtins.)

Nope, the PHP Parser isn't very fussy when it comes to TRUE, true and FALSE, false

http://php.net/manual/en/language.types.boolean.php
To specify a boolean literal, use the keywords TRUE or FALSE. Both are case-insensitive.

Related

VB.Net true and false Operators

Does VB.Net have true and false operators like C# does?
Until this morning, I was not aware that C# has true and false "operator"s.
I found them listed in a book, along with the normal operators (&, ^, |, &&, etc.).
At first I thought it was a misprint; I thought that true and false were only Boolean values, not operators.
I found this MSDN page: true Operator (C# Reference) about C#'s true operator, but was unable to find anything about VB's True operator (if it even exists).
Yes, it has the IsTrue and IsFalse overload operators.
Sure, they are True and False.
But note that True in VB.NET evaluates to -1.
VB.NET also has the logical operators Not, And, Xor, &c.

Setting boolean values in the DB

The way that boolean values are stored varies from database to database. Some use 1 and 0 integer values to represent true and false, respectively. Others use characters such as T and F.
How do I choose which to use? 1 and 0, or T and F? And likewise, how do I set them?
Please help clear this up for me!
Thanks!!
Don't try to do the work of the DB adapters: depending on which you've set (one for MySQL, one for PostgreSQL, etc.), they will know what to write.
User.create(is_admin: false) # use Ruby's boolean classes,
# the adapter will translate it in its own language to persist it in the DB
In the same way, the reverse will work perfectly:
User.first.is_admin # => return `true` or `false` (objects of Ruby, not a string like 'true' or 1 or 'T' or whatever)
Use true or false for Boolean, DB adapters handles the rest, in setting just mention if #model_object is an object of model then you can sat it like this #model_object.attribute = false or true

String comparison is case sensitive in Expression language?

So just as simple as that. Comparing two strings that differs only for case
${"a" == "A"}
returns true or false?
When two strings are compared in EL, the comparison is always case-sensitive. If you want to do a string comparison that's not case-sensitive, you can just use the equalsIgnoreCase method.
That comparison would return false.
It's case sensitive, so it will return false. Another possibility to do a case insensitive comparison of strings is using JSTL functions:
${fn:toLowerCase(stringA) == fn:toLowerCase(stringB)}
This is useful for older versions of EL. See https://stackoverflow.com/tags/el/info and scroll to "Invoking non-getter methods".

Semantics of the Boolean and comparison operators in the context of three-valued-logic

Professor ask me this question:
What is the semantics of the Boolean and comparison operators in the context of three-valued-logic?
I'm not sure what he meant by that. What is comparison operator? Is that the same as relational operator? Semantics? He asking about 'meaning' of those two terms in context of three-valued-logic?
Thanks.
Boolean values by definition consist of two options: True and False, or some representation of them like 1/0, ON/OFF, YES/NO, etc.
Many database systems also allow you to store a NULL value in fields designated as Boolean to allow them to represent three values: Yes, No, and Unknown (NULL).
The Boolean Operators are AND, OR, and NOT.
Comparison Operators are some form of EQUALS or NOT EQUALS.
Operations with TRUE/FALSE values on both ends are obvious:
TRUE or FALSE -> TRUE
NOT TRUE -> FALSE
TRUE=TRUE -> TRUE
What he is getting at are the consequences of adding the NULL (Unknown) value:
TRUE or UNKNOWN(NULL) -> ???
NOT UNKNOWN(NULL) -> ???
TRUE=UNKNOWN(NULL) -> ???
http://en.wikipedia.org/wiki/Null_(SQL)#Three-valued_logic_.283VL.29
Basically, three value logic is true/false/null, and the boolean/comparison operators will function in certain ways when comparing true == null, null == null, etc.
Boolean logic by definition uses only two values. To me, this question doesn't make sense. If he would asked how can you define or extend Boolean logic so that it could use three-value system, it would be clearer.

Not keyword vs = False when checking for false boolean condition

When I'm using an If statement and I want to check if a boolean is false should I use the "Not" keyword or just = false, like so
If (Not myboolean) then
vs
If (myboolean = False) then
Which is better practice and more readable?
Definitely, use "Not". And for the alternately, use "If (myboolean)" instead of "If (myboolean = true)"
The works best if you give the boolean a readable name:
if (node.HasChildren)
Since there's no functional difference between either style, this is one of those things that just comes down to personal preference.
If you're working on a codebase where a standard has already been set, then stick to that.
Use True and False to set variables, not to test them. This improves readability as described in the other answers, but it also improves portability, particularly when best practices aren't followed.
Some languages allow you to interchange bool and integer types. Consider the contrived example:
int differentInts(int i, int j)
{
return i-j; // Returns non-zero (true) if ints are different.
}
. . .
if (differentInts(4, 8) == TRUE)
printf("Four and Eight are different!\n");
else
printf("Four and Eight are equal!\n");
Horrible style, but I've seen worse sneak into production. On other people's watches, of course. :-)
Additionally to the consensus, when there is both a true case and a false case please use
if (condition)
// true case
else
// false case
rather than
if (not condition)
// false case
else
// true case
(But then I am never sure if python's x is not None is the true-case or the false case.)
Definitely use "Not", consider reading it aloud.
If you read aloud:
If X is false Then Do Y
Do Y
Versus
If Not X Then Do Y
I think you'll find the "Not" route is more natural. Especially if you pick good variable names or functions.
Code Complete has some good rules on variable names. http://cc2e.com/Page.aspx?hid=225 (login is probably required)
! condition
In C and pre-STL C++, "!condition" means condition evaluates to a false truth value, whereas "condition == FALSE" meant that the value of condition had to equal what the system designed as FALSE. Since different implementations defined it in different ways, it was deemed better practice to use !condition.
UPDATE: As pointed out in the comment -- FALSE is always 0, it's TRUE that can be dangerous.
Something else: Omit the parentheses, they’re redundant in VB and as such, constitute syntactic garbage.
Also, I'm slightly bothered by how many people argue by giving technical examples in other languages that simply do not apply in VB. In VB, the only reasons to use If Not x instead of If x = False is readability and logic. Not that you’d need other reasons.
Completely different reasons apply in C(++), true. Even more true due to the existence of frameworks that really handle this differently. But misleading in the context of VB!
It does not make any difference as long you are dealing with VB only, however if you happen to use C functions such as the Win32 API, definitely do not use "NOT" just "==False" when testing for false, but when testing for true do not use "==True" instead use "if(function())".
The reason for that is the difference between C and VB in how boolean is defined.
In C true == 1 while in VB true == -1 (therefore you should not compare the output of a C function to true, as you are trying to compare -1 to 1)
Not in Vb is a bitwise NOT (equal to C's ~ operator not the ! operator), and thus it negates each bit, and as a result negating 1 (true in C) will result in a non zero value which is true, NOT only works on VB true which is -1 (which in bit format is all one's according to the two's complement rule [111111111]) and negating all bits [0000000000] equals zero.
For a better understanding see my answer on Is there a VB.net equivalent for C#'s ! operator?
Made a difference with these lines in vb 2010/12
With the top line, Option Strict had to be turned off.
If InStr(strLine, "=") = False Then _
If Not CBool(InStr(strLine, "=")) Then
Thanks for answering the question for me. (I'm learning)