I noticed that some PHP frameworks use exclusively lowercase true/false and others upper.
Does it make any difference at all? I for one prefer lowercase.
No difference, some people consider FALSE to be a constant and thus use the old screaming caps notation.
It's been some time but because this is the first search result for the query I thought this info would be useful.
According to the PSR-2 Coding Style Guide
PHP keywords MUST be in lower case. The PHP constants true, false,
and null MUST be in lower case.
Even though you can use the screaming caps notation; if you are trying to follow these guidelines you should be doing it in lowercase. There will be no functional difference either way.
I know I'm late to the party here, but I'll just quote the documentation in case anybody wants an authoritative answer, as I did.
To specify a boolean literal, use the constants TRUE or FALSE. Both are case-insensitive.
In fact, the example immediately following the above uses this fact:
$foo = True; // assign the value TRUE to $foo
PhP is biZarRe.
Related
One of my colleagues made a post that said something like this:
In PHP, if you have two variables referring to the same value, they are the same instance.
$a="Mary";
$b="Mary";
$c="lamb"
He implies that $a and $b refer to the same instance(memory space). I am having trouble beleiving this. I know that this is somewhat true in java, but I don't think its so for php, since in php strings aren't actually immutable by principle, it would not make sense to have one instance
Further,he said, if we do unset($a) it only removes the reference of $a not the actual value.
This is ofcourse true, but proves nothing
I also tried the following code and printed both $a and $b. If they were sharing the same instance, the value of $b would have changed too.
$a[2]=3;
echo "<br/>\$a: $a<br/>"; //He3lo
echo "<br/>\$b: $b<br/>";//Hello
I would love to check the memory space of the variables, but I don't think php allows to do that. Can somebody clarify if this is true
You are referring to a concept called String interning. It seems that it is implemented in the Zend Engine since Version 5.4: "Our implementation makes the strings which are known at compile-time interned." Source.
I'm using filter_var to validate boolean values but I did not expect it to not recognize FALSE. Why does this happen?
filter_var(FALSE, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
returns
null
filter_var is new as of PHP 5.2. You've run into a known bug: https://bugs.php.net/bug.php?id=49510
Feel free to vote on or comment on that bug.
You're trying to do something like this:
$v = filter_var($v, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
There are a number of cheap workarounds like this:
$v = $v===FALSE ? FALSE : filter_var($v, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
It sounds like this is actually how it's supposed to work, strangely enough (yes, my mind was blown by that). From https://bugs.php.net/bug.php?id=51344
This is going to sound insane when you've looked at the underlying
filter code, but this is actually correct according to the
documentation: the default behaviour of filter_input() is to return
NULL for non-existent inputs and false when validation fails, and
FILTER_NULL_ON_FAILURE simply flips that behaviour to false for
non-existent inputs and NULL on validation failure. (No, I don't have
a clue where that would be useful either, and the name of the flag is
unfortunate in the filter_input() context, since it implies that NULL
wouldn't normally be returned. It makes more sense when used with
filter_var(), which doesn't have the non-existent input case.)
[table omitted due to SO formatting]
I'll pop a comment into the filter_input() and filter_input_array()
implementations to note that this is by design, even though the code
does kind of look wrong.
Closing Won't Fix.
This was the behaviour when filter_var was first introduced with version 5.2 and resolved at some point after 5.4 as is seen by this https://3v4l.org/Cv1MZ
Starting from version 5.4 this is what happens:
var_dump(filter_var(FALSE, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
bool(false)
which makes much more sense.
According to the documentation
Returns true for "1", "true", "on" and "yes". Returns false otherwise.
Anything different than the values mentioned above is considered falsy. This is not to test if a variable is actually a boolean like with typeof or is_bool(), but more like a mean to test if the input comming from the user (usually an <input type="checkbox"> form) is truthy/falsy.
The behavior of this function could be understand as let it go through if it's correct rather than a mean to test the type of a variable (we have lots of other functions for this). Exemple :
filter_var('435345', FILTER_VALIDATE_INT)
The type is a string, but the result is not true/false as the intention is not to validate the type. So an int would be returned (let go through).
I've seen a lot of people using
defined('XXX') or define('XXX', 'XXX');
instead of
if(!defined('XXX')){
define('XXX', 'XXX');
}
Does the first code do exactly the same thing? Why do people use it?
The feature is called short circuit evaluation and it's common to many languages. Boolean expressions are evaluated from left to right and evaluation stops when there's already a result. In this case, if the constant is defined the expression is TRUE no matter the other term, so define() does not run.
They do exactly the same thing. The first is just shorter to write. Similar to using
mysql_connect(...) or die('some error');
The right side of the logical OR is evaluated only if the left side is FALSE.
Does the exact same thing. Basically its (TRUE CONDITION) or FALSE ALTERNATIVE
It does exactly the same, relying on the fact that logical OR requires evaluation of the second operand if the first evaluates to FALSE.
I wouldn't use this method too broadly, as it tends to "short-circuit" conditionals (i.e. TRUE or f(); - f() will never be called)
Both of these will ensure than $var is a boolean value, but the latter seems more clear. The double exclamation mark (!!) is shorter to type but less clear, and more likely to cause confusion. Not to mention hard to run a search on to get answers.
The double exclamation mark is something I've only heard of in JavaScript, which doesn't have boolean typecasting. Is it normal to see it used in PHP as well?
This is valid in JavaScript, although not technically a "cast", it achieves the same effect:
var booleanValue = Boolean(otherValueType);
This is equivalent to:
var booleanValue = !!otherValueType;
I find it is good to do this when processing incoming parameters, to clarify that one intended a value to be a boolean. When checking for "truthiness", there is no need to typecast in either PHP or JavaScript. Just remember that an empty string is equivalent to false in PHP and true in JavaScript.
So, to answer your question, either is fine in either language, it is simply a personal style choice.
Neither of those is common in PHP because they're unnecessary.
If you can do !!, you can just as well use it where a boolean expression is necessary (while, if, &&, etc.).
in Yii they use this code:
defined('YII_DEBUG') or define('YII_DEBUG',true);
i've never seen anyone write like this before (or). is this real php code or some syntax of yii?
Basically if defined("YII_DEBUG") evaluates to false, it will then define it. Kinda like:
mysql_connect() or die("DIE!!!!!");
It is actual PHP syntax, just not commonly used. It allows you to not have to write:
if(!defined("YII_DEBUG"))
{
define("YII_DEBUG", true);
}
or even shorter
if(!defined("YII_DEBUG"))
define("YII_DEBUG", true);
I'm guessing they used it to get rid of the if statement altogether. The second if statement without brackets would be a hazard for editing, and the first one might have taken up too much room for the developer.
Personally, I'd stick clear of this just because it isn't a commonly known feature. By using commonly known syntaxes (the if statements), other programmers don't have to wonder what it does.
(Although, I might now that I look at it. Seems straightforward and gets rid of unnessecary if clauses)
It's due to short circuit evaluation.
If defined('YII_DEBUG') returns false, it will try to evaluate the second expression to make the sentence true, defining YII_DEBUG constant as true.
The final result is that, if the constant were not defined, then define it as being true. If it's already defined (the value doesn't matter), then do nothing (as the first expression is true, the second expression does not need to be evaluated for the expression to be true).
Pretty straightforward - the 'or' operator is efficient in that it will only evaluate the second part of the statement if it needs to. So if the first part evaluates to true (the constant is already defined), then the define() call isn't executed.