Results 1 to 9 of 9

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

  1. #1
    Join Date
    Oct 2010

    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:

    	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); -

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

    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
    Have you tried

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

  3. #3
    Join Date
    Oct 2010
    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
    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:
    Last edited by aj_nsc; 05-07-2011 at 08:38 AM.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...

  5. #5
    Join Date
    Oct 2010
    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:

    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
    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).
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...

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

    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
    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):

    dataObj = {};
          if(document.getElementById("photos_checkbox").checked) { dataObj['photos'] = true; };
       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 09:48 AM.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...

  9. #9
    Join Date
    Oct 2010
    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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11358 seconds
  • Memory Usage 2,934KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (7)bbcode_code
  • (3)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (9)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates