doubledee Posted January 7, 2012 Share Posted January 7, 2012 I am wondering if these "true" values are the same to PHP... <a href="' . BASE_URL . 'members/log_in.php?addComment=true">Log In</a> if (isset($_GET['addComment']) && ($_GET['addComment']==TRUE)){ Debbie Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/ Share on other sites More sharing options...
trq Posted January 7, 2012 Share Posted January 7, 2012 Not exactly, all values passed via the querystring are strings. Having said that however, there are a number of values that will return true in php. See this page: http://php.net/manual/en/language.types.boolean.php Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305370 Share on other sites More sharing options...
doubledee Posted January 7, 2012 Author Share Posted January 7, 2012 Not exactly, all values passed via the querystring are strings. Having said that however, there are a number of values that will return true in php. See this page: http://php.net/manual/en/language.types.boolean.php Pretty lose way to define "TRUE"?! Should I type-cast my query string value, or is what I am doing above save enough? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305373 Share on other sites More sharing options...
laffin Posted January 7, 2012 Share Posted January 7, 2012 $addcomment= (isset($_GET['addComment']) && ($_GET['addComment']=='TRUE')?TRUE:FALSE; or if (isset($_GET['addComment']) && ($_GET['addComment']=='TRUE') $addcomment=TRUE; else $addcomment=FALSE; Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305376 Share on other sites More sharing options...
kicken Posted January 7, 2012 Share Posted January 7, 2012 Should I type-cast my query string value, or is what I am doing above save enough? Do you plan on ever using &addcomment=false? If not, then I would just stick with the isset() check and ignore what the value is. If you do think you'll be using a false value, then I would do the string comparison: $addcomment = (isset($_GET['addcomment']) && $_GET['addcomment']=='true'); Note that typecasting to a bool would not yield a true result because the string is 'true', but because it's not '' (empty) or '0' (zero). For instance, the following: $value = 'false'; $bool = (bool)$value; $bool would be true, not false. So if you do pass a 'true'/'false' you need to test them as a string, not just typecast. If you instead use '1'/'0' then you can typecast to either bool or int and it will work ok. Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305380 Share on other sites More sharing options...
doubledee Posted January 7, 2012 Author Share Posted January 7, 2012 Here is the code I went with... // ****************** // Redirect User. * // ****************** // Add a Comment Redirect. if (isset($_GET['addComment']) && ($_GET['addComment']==TRUE)){ header("Location: " . BASE_URL . "articles/add_comment.php"); // End script. exit(); } // Normal Redirect. if (isset($_SESSION['returnToPage'])){ header("Location: " . BASE_URL . $_SESSION['returnToPage']); }else{ // Take user to Home Page. header("Location: " . BASE_URL . "index.php"); } Debbie Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305387 Share on other sites More sharing options...
scootstah Posted January 7, 2012 Share Posted January 7, 2012 Here is the code I went with... // ****************** // Redirect User. * // ****************** // Add a Comment Redirect. if (isset($_GET['addComment']) && ($_GET['addComment']==TRUE)){ header("Location: " . BASE_URL . "articles/add_comment.php"); // End script. exit(); } // Normal Redirect. if (isset($_SESSION['returnToPage'])){ header("Location: " . BASE_URL . $_SESSION['returnToPage']); }else{ // Take user to Home Page. header("Location: " . BASE_URL . "index.php"); } Debbie With this code, if "$_GET['addComment']" is equal to ANYTHING, the check will pass. If you only want it to pass when it is set to "true", then you need to do: if (isset($_GET['addComment']) && ($_GET['addComment']=='true')){ Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305391 Share on other sites More sharing options...
doubledee Posted January 7, 2012 Author Share Posted January 7, 2012 With this code, if "$_GET['addComment']" is equal to ANYTHING, the check will pass. If you only want it to pass when it is set to "true", then you need to do: if (isset($_GET['addComment']) && ($_GET['addComment']=='true')){ Oops!! Would it be better if I did things like this... echo '<p>To leave a comment, you need to...</p> <ul class="button2"> <li> <a href="' . BASE_URL . 'members/log_in.php?addComment=1">Log In</a> </li> Debbie Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305393 Share on other sites More sharing options...
trq Posted January 7, 2012 Share Posted January 7, 2012 It's not a major issue. Just check for the string "true". Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305394 Share on other sites More sharing options...
trq Posted January 7, 2012 Share Posted January 7, 2012 Or really, you could just check that $_GET['addComment'] exists if it's only ever going to have one value. Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305395 Share on other sites More sharing options...
scootstah Posted January 7, 2012 Share Posted January 7, 2012 With this code, if "$_GET['addComment']" is equal to ANYTHING, the check will pass. If you only want it to pass when it is set to "true", then you need to do: if (isset($_GET['addComment']) && ($_GET['addComment']=='true')){ Oops!! Would it be better if I did things like this... echo '<p>To leave a comment, you need to...</p> <ul class="button2"> <li> <a href="' . BASE_URL . 'members/log_in.php?addComment=1">Log In</a> </li> Debbie No. 1 is not a boolean. It will evaluate to true, but any value will do the same. If you want it to only ever work if it says ?addComment=true, then you need to check that the string is "true". Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305403 Share on other sites More sharing options...
.josh Posted January 8, 2012 Share Posted January 8, 2012 Pretty lose way to define "TRUE"?! Well php is a loosely (weak) typed language... Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305411 Share on other sites More sharing options...
doubledee Posted January 8, 2012 Author Share Posted January 8, 2012 Or really, you could just check that $_GET['addComment'] exists if it's only ever going to have one value. True. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305416 Share on other sites More sharing options...
doubledee Posted January 8, 2012 Author Share Posted January 8, 2012 No. 1 is not a boolean. It will evaluate to true, but any value will do the same. If you want it to only ever work if it says ?addComment=true, then you need to check that the string is "true". Yeah, I think I'll just do... $_GET['addComment']=='true' Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305417 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.