www.webdeveloper.com
Results 1 to 9 of 9

Thread: if(false==true) returns true?!

  1. #1
    Join Date
    Oct 2010
    Posts
    17

    if(false==true) returns true?!

    Well, not quite as weird as that! I'm processing the results of a form submitted with GET. I'm then collecting together all of the values in the $_GET array which are set and putting them into variables which are then the arguments of another function which makes a query to a database. Here's a snippet:

    Code:
    	if($_GET['photos'])	$restrict_type[]='photos';
    	if($_GET['videos'])	$restrict_type[]='videos';
    	if($_GET['producer'])	$producer=$_GET['producer'];
    	if($_GET['consumer'])	$consumer=$_GET['consumer'];
    	if($_GET['distance'])	$location=$_GET['distance'];
    
    ...
    
    	print_r($_GET); // this is here to help my error-checking
    	
    ...
    	$requestArr=getRequestArray($producer,$consumer,$location,$restrict_type ...);
    $_GET['photos'] and $_GET['videos'] come from checkbox elements. My problem is that even when print_r confirms that these values == false, the if statements always return true. To absolutely confirm where the error was I put in a line before print_r($_GET); -

    Code:
    if($_GET['photos']==true){echo 'get photos is set to true';}
    The output from those two lines that I get is:

    Code:
    get photos is set to true
    Array ( [photos] => false [videos] => true )
    I'm clearly missing something extremely basic here! Can anyone tell me what it is?

  2. #2
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Have you tried

    PHP Code:
     if(isset($_GET['photos'])) {
    //do something


  3. #3
    Join Date
    Oct 2010
    Posts
    17
    I haven't tried isset() because $_GET['photos'] etc are always set - if the checkbox is unticked then they're set to false, not NULL.

  4. #4
    Join Date
    Dec 2005
    Posts
    2,984
    Quote Originally Posted by tzirtzi View Post
    I haven't tried isset() because $_GET['photos'] etc are always set - if the checkbox is unticked then they're set to false, not NULL.
    That's a problem, unchecked checkboxes aren't even be passed to the script, so you must be setting them to "false" manually if the specified $_GET variable isn't set - which is silly, because why set something that is already null (which == false) to "false"?

    Brings me to my next point, $100 says $_GET['photos'] is not equal to false, but "false" (a non empty, non-"0" string). Non-empty strings, of course, return true.

    Instead of using print_r($_GET) use var_dump($_GET) and you'll see what I mean. It'll say something like this:

    PHP Code:
    array(1) { ["photos"]=> string(5"false" 
    Scroll down just pass the warning on the following page and have a gander:
    http://php.net/manual/en/language.types.boolean.php
    Last edited by aj_nsc; 05-07-2011 at 09:38 AM.

  5. #5
    Join Date
    Oct 2010
    Posts
    17
    You win $100! (figuratively, that is). Thankyou very much for that suggestion.

    This file is being called by Ajax - so all of these GET variables are being passed through javascript. Something like:

    Code:
    photos = document.getElementById("photos_checkbox").checked;
    $("#content").load("./include/filename.php?photos="+photos+"&videos="+videos+..., Hide_Load());
    Is there any chance that this is responsible for changing NULL into "false"?

    I'll experiment...

  6. #6
    Join Date
    Dec 2005
    Posts
    2,984
    Good thing to point out it's being called by AJAX. That way, you are 100% in control of what is being passed.

    The way the AJAX call is written, photos will always be a string with a value of either true, or false (the return value of document.getElementById("photos_checkbox").checked; true if it is checked and false if it isn't checked).

    The thing is, the Javascript variable photos contains a boolean Javascript value, but when you stick it in a string like you have in your AJAX request, Javascript converts the whole thing to a string (the boolean is no longer a boolean, but part of a string, so the boolean itself becomes a string).

    When I said above that unchecked checkboxes aren't even passed to the server, that's only in the case of a direct form submission (no AJAX).

  7. #7
    Join Date
    Oct 2010
    Posts
    17
    Okay, thanks very much, that certainly makes sense. The most obvious solution, it seems to me, is to put a line in the js:

    Code:
     if(!photos){photos=0;}
    For this and all similar variables - if they have to be passed as strings then they need to contain a string value that php will evaluate as false, and "0" is such a value. Obviously the other possibility would be to write php to change all "false" in the $_GET array into FALSE, but this way seems preferable to me.

    I suppose another possibility would be to use a POST instead of GET - am I correct in thinking that you can pass non-string variables using POST?

  8. #8
    Join Date
    Dec 2005
    Posts
    2,984
    Code:
    photos = (document.getElementById("photos_checkbox").checked) ? true : 0;
    My current understand of XHR is that everything is a string when it's passed. PHP obviously let's you loosely compare values so if you pass the string "1" and do:

    PHP Code:
    if($_GET['photos'] == 1
    You'll get true, because "1" == 1 will return true but "1" === 1 will return false.

    Regardless of POST or GET format, XHR sends everything as string(s) and types are lost.

    Personally, to keep AJAX triggered PHP scripts compatible with the handling the regular form request (as a fallback in case JS isn't enabled) I'd prefer to keep the handling of forms the same. Like I said above, unchecked checkboxes and radio buttons are not passed to the server when you submit a form. For instances, if the photos checkbox wasn't checked, and you submitted the form without AJAX, the index 'photos' wouldn't even exist in the $_GET (or $_POST, depending on your method) super global.

    For that reason, I tend to handle my AJAX requests the following way (using jQuery as an example):

    Code:
    dataObj = {};
          if(document.getElementById("photos_checkbox").checked) { dataObj['photos'] = true; };
    
    $.ajax({
       cache : false,
       url : 'path/to/script.php',
       data : (dataObj),
       type: 'POST', //or 'GET'
       success : function(r) {
         //r contains the response
       }
    });
    That way, if the photos checkbox wasn't checked, dataObj['photos'] wouldn't even exist in the information passed to the script - exactly like the way the server would handle it if you submitted the form without AJAX.

    This is a lot of 'in an ideal world' talk though. What it comes down to is you doing something that is a) consistent and b) easily readable/understandable in your applications.
    Last edited by aj_nsc; 05-07-2011 at 10:48 AM.

  9. #9
    Join Date
    Oct 2010
    Posts
    17
    Or hey, that's much more concise

    Thank you again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles