Boolean not holding value
I've written an "abstraction layer" class for form parsing. There are many functions inside the class that create various elements of a form.
In these functions is the ability to define what the inputted values should be. If the inputted value does not match the specified pattern, it returns as false and adds the original inputted value to an array of false-inputs for calling that data if need be.
The code works, besides this problem, for when I change it to assign it as a string 'false' it sets it so.
HOWEVER, when I'm setting it as a boolean it does NOT retain that value of false. It instead has no value.
I have tried specifically setting the variable as a boolean through settype(), but to no avail.
Again, wrapping that false value with quotes seems to make the variable retain its' value. Whereas without the quotes it does not retain its' value.
It appears that a value of true is turned into a value of 1 and a value of false is turned into a value of nothing.
Is this a default php.ini setting? How do I prevent this re-assigning from happening?
Can you show the code where you actually compare the true/false value?
Try with a strict comparison: ===
The problem is that PHP doesn't actually have a boolean, which is inherited from the C code which also doesn't have a boolean. Ultimately, boolean false is 0, and true is !0.
Fortunately, operations can use strict datatyping in PHP. Using identical operators (===) for individual comparisons, and using in_array with strict parameter checking (third parameter), are good options. Searching arrays can also be performed on strict checking. The identical check also performs a check on the zval type on top of the zval value (default is to check only the value).
You'll get to know the identical operator in PHP. Many functions especially string and array functions return false on failure. Since PHP arrays (and implicitly the strings since they are char arrays) are all 0 based, you need to use identical checking for false to see if it failed or not. Common operations for these are strpos and array_search.
Thanks guys, you answered my question perfectly! :thumbsup:
|All times are GMT +1. The time now is 04:38 PM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.