Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts

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

    PHP Code:
    protected function parseForm(){
        
    $this->rRaw array_merge($_POST$_GET);
        foreach(
    $this->rRaw as $key => $value){
            if(
    is_array($value)){
                foreach(
    $value as $validator => $content){
                    if(
    strpos($validator'formval_')){
                        if(
    $this->validateForm(substr($validator9, -1), $content)){
                            if(
    strpos($validator'noclean_')){
                                
    $this->r[$key] = $content;
                            }else{
                                
    $this->r[$key] = $this->cleanData($content);
                            }
                        }else{
                            
    $this->r[$key] = false;
                            if(
    strpos($validator'noclean_')){
                                
    $this->r['false-inputs'][$key] = $content;
                            }else{
                                
    $this->r['false-inputs'][$key] = $this->cleanData($content);
                            }
                        }
                    }else{
                        
    $this->r[$key][$validator] = (strpos($validator'noclean_') ? $this->r[$key] = $content $this->r[$key] = $this->cleanData($content));
                    }
                }
            }else{
                
    $this->r[$key] = $this->cleanData($value);
            }
        }
        
    $this->checkQueryString();

    My problem is that when the user-inputted values do no pass the validation process line 14 of the above code is supposed to assign the variable as a boolean and set it to false.

    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.
    Last edited by TFlan; 01-04-2013 at 10:04 PM.

  • #2
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    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?

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Can you show the code where you actually compare the true/false value?

    Try with a strict comparison: ===
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    TFlan (01-07-2013)

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by AndrewGSW View Post
    Can you show the code where you actually compare the true/false value?

    Try with a strict comparison: ===
    ^ This
    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.

  • Users who have thanked Fou-Lu for this post:

    TFlan (01-07-2013)

  • #5
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    Thanks guys, you answered my question perfectly!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •