Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-04-2013, 09:39 PM   PM User | #1
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
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..
TFlan is offline   Reply With Quote
Old 01-04-2013, 09:52 PM   PM User | #2
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
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?
TFlan is offline   Reply With Quote
Old 01-05-2013, 01:56 AM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
TFlan (01-07-2013)
Old 01-05-2013, 05:54 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
TFlan (01-07-2013)
Old 01-07-2013, 08:29 PM   PM User | #5
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
Thanks guys, you answered my question perfectly!
TFlan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:58 AM.


Advertisement
Log in to turn off these ads.