doubledee Posted June 17, 2012 Share Posted June 17, 2012 In this code... if ($preview==999){ echo '<div id="boxPreview"> <h3>Preview</h3> <p>' . (isset($comments) ? nl2br(str2htmlentities($comments)) : "") . '</p> </div>'; } If $preview has a value of "1", then why would my If-Then evaluate to TRUE and my echo code fire??? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/ Share on other sites More sharing options...
Pikachu2000 Posted June 17, 2012 Share Posted June 17, 2012 Because it obviously doesn't equal 1, it equals 999. That's the only reason. Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354683 Share on other sites More sharing options...
doubledee Posted June 17, 2012 Author Share Posted June 17, 2012 Because it obviously doesn't equal 1, it equals 999. That's the only reason. Huh?????? This statement is FALSE... 1==999 And so in this code, my echo should never fire... if ($preview==999){ echo '<div id="boxPreview"> <h3>Preview</h3> <p>' . (isset($comments) ? nl2br(str2htmlentities($comments)) : "") . '</p> </div>'; } Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354685 Share on other sites More sharing options...
Pikachu2000 Posted June 17, 2012 Share Posted June 17, 2012 Again, the only reason that condition would evaluate to TRUE is if $preview is 999. There is no other reason. Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354686 Share on other sites More sharing options...
doubledee Posted June 17, 2012 Author Share Posted June 17, 2012 Again, the only reason that condition would evaluate to TRUE is if $preview is 999. There is no other reason. As I said in my OP, $preview=1... (Specifically, I set $preview=TRUE, and so it evaluates to "1" as verified by me and NetBeans...) If I have literals in there it fails... if (1==999){ But this code... if ($preview==999){ ...takes the THEN branch meaning the interpreter sees it as TRUE... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354687 Share on other sites More sharing options...
Pikachu2000 Posted June 17, 2012 Share Posted June 17, 2012 Because it obviously doesn't equal 1, it equals 999. That's the only reason. Again, the only reason that condition would evaluate to TRUE is if $preview is 999. There is no other reason. Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354688 Share on other sites More sharing options...
Drummin Posted June 17, 2012 Share Posted June 17, 2012 If you're using TRUE/FALSE, then I suggest you also use that in your IF condition. <?php $preview=TRUE; if ($preview==TRUE){ echo '<div id="boxPreview"> <h3>Preview</h3> <p>' . (isset($comments) ? nl2br(str2htmlentities($comments)) : "") . '</p> </div>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354689 Share on other sites More sharing options...
PFMaBiSmAd Posted June 17, 2012 Share Posted June 17, 2012 If your comparison is actually if(TRUE==999), rather than the previously stated if (1==999), then yes, TRUE is == to 999. Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354691 Share on other sites More sharing options...
doubledee Posted June 17, 2012 Author Share Posted June 17, 2012 Because it obviously doesn't equal 1, it equals 999. That's the only reason. How does 1==999 equal 999 ?????????????????? Again, the only reason that condition would evaluate to TRUE is if $preview is 999. There is no other reason. Jerking my chain?? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354693 Share on other sites More sharing options...
Pikachu2000 Posted June 17, 2012 Share Posted June 17, 2012 Jerking your chain? No. I'm telling you, based on the code you posted yourself, that the only way that condition will evaluate TRUE is if $preview is 999, NOT 1, as you so adamantly claim it to be. Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354694 Share on other sites More sharing options...
doubledee Posted June 17, 2012 Author Share Posted June 17, 2012 If your comparison is actually if(TRUE==999), rather than the previously stated if (1==999), then yes, TRUE is == to 999. How is TRUE == 999 ??????? This code results in FALSE... if (1==999){ echo 'TRUE'; }else{ echo 'FALSE'; } In *my* code, I have... }elseif (isset($_POST['preview'])){ $preview = TRUE; }//End of PROCESS FORM And I type in $preview==999 last night - and forgot about it - to force a FALSE condition which was what happened last night in this code... <!-- PREVIEW COMMENT --> <?php if ($preview==999){ echo '<div id="boxPreview"> <h3>Preview</h3> <p>' . (isset($comments) ? nl2br(str2htmlentities($comments)) : "") . '</p> </div>'; } ?> Today when I step through my code in NetBeans, if I click the "Preview" button, $preview=TRUE; and I would expect my echo to not fire because "1" - which is what NetBeans shows in the $preview variable - does NOT equal "999"... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354695 Share on other sites More sharing options...
doubledee Posted June 17, 2012 Author Share Posted June 17, 2012 Jerking your chain? No. I'm telling you, based on the code you posted yourself, that the only way that condition will evaluate TRUE is if $preview is 999, NOT 1, as you so adamantly claim it to be. I am seeing it happen with my own two eyes in NetBeans as I step through my code... NetBeans says $preview is "1" in the variables tab, and that would mean I have 1 == 999 for $preview == 999 and that SHOULD BE FALSE, but my If-Then is taking the Then path meaning that 1 == 999 is evaluating to TRUE?! (Like I said above, that was testing code that should have been removed last night, but I still would like to know what is going own for my own sanity!!) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354697 Share on other sites More sharing options...
KevinM1 Posted June 17, 2012 Share Posted June 17, 2012 NetBeans is simply showing you the integer value of true, which is 1. It's not actually the number 1 in this case, since you set $preview to logical true. It's just the numerical representation of true. Why is true represented as a 1? Think binary. 1 == on/true, 0 == off/false. In most modern programming languages, any non-zero value that can be converted to an integer will be true. So, True == 999 will always result in true. FWIW, this is boolean logic 101. One of those things you should learn if you're going to use if/else statements. Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354699 Share on other sites More sharing options...
Drummin Posted June 17, 2012 Share Posted June 17, 2012 Not sure if this applies to this. From http://php.net/manual/en/language.types.boolean.php Warning -1 is considered TRUE, like any other non-zero (whether negative or positive) number! When converting to boolean, the following values are considered FALSE: the boolean FALSE itself the integer 0 (zero) the float 0.0 (zero) the empty string, and the string "0" an array with zero elements an object with zero member variables (PHP 4 only) the special type NULL (including unset variables) SimpleXML objects created from empty tags Every other value is considered TRUE (including any resource). Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354700 Share on other sites More sharing options...
Pikachu2000 Posted June 17, 2012 Share Posted June 17, 2012 I really don't give a red rat's ass what NetBeans says. Why don't you show us where you assign the value to $preview? Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354701 Share on other sites More sharing options...
doubledee Posted June 17, 2012 Author Share Posted June 17, 2012 NetBeans is simply showing you the integer value of true, which is 1. It's not actually the number 1 in this case, since you set $preview to logical true. It's just the numerical representation of true. Why is true represented as a 1? Think binary. 1 == on/true, 0 == off/false. I get that. In most modern programming languages, any non-zero value that can be converted to an integer will be true. I get that. For example, this would evaluate to TRUE... if (999){ echo 'This line will be shown'; }else{ echo 'This line will not be shown'; } So, True == 999 will always result in true. Above you said "1" and "TRUE" were the same. And I get that most If-Then's treat any Positive Number as TRUE. But I still do not see how "TRUE" can be logically equal to "999" ?! (This I can see... TRUE==1) FWIW, this is boolean logic 101. One of those things you should learn if you're going to use if/else statements. I've never encountered this situation before, so not so "101"... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354704 Share on other sites More sharing options...
doubledee Posted June 17, 2012 Author Share Posted June 17, 2012 I really don't give a red rat's ass what NetBeans says. Why don't you show us where you assign the value to $preview? Get you Old-Man Glasses out and maybe you'll see that I posted my code in Reply #10... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354705 Share on other sites More sharing options...
Pikachu2000 Posted June 17, 2012 Share Posted June 17, 2012 I really don't give a red rat's ass what NetBeans says. Why don't you show us where you assign the value to $preview? Get you Old-Man Glasses out and maybe you'll see that I posted my code in Reply #10... Debbie So then if you knew you assigned the boolean value of TRUE to the $preview variable, why did you continue to insist that you had given it the integer value of 1? Alzheimer's disease or just plain ignorance? Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354707 Share on other sites More sharing options...
KevinM1 Posted June 17, 2012 Share Posted June 17, 2012 In most modern programming languages, any non-zero value that can be converted to an integer will be true. But I still do not see how "TRUE" can be logically equal to "999" ?! (This I can see... TRUE==1) True is logically equal to all non-zero values. 23.987. 999. "Hello". All of those will be equal to true. Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354711 Share on other sites More sharing options...
doubledee Posted June 17, 2012 Author Share Posted June 17, 2012 I really don't give a red rat's ass what NetBeans says. Why don't you show us where you assign the value to $preview? Get you Old-Man Glasses out and maybe you'll see that I posted my code in Reply #10... Debbie So then if you knew you assigned the boolean value of TRUE to the $preview variable, why did you continue to insist that you had given it the integer value of 1? I said NetBeans showed "1" and you guys are saying "TRUE" is the same as "1" Alzheimer's disease or just plain ignorance? Nah, I just like to give you stuff to bitch about... Freebie from me for the curmudgeon... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354712 Share on other sites More sharing options...
doubledee Posted June 17, 2012 Author Share Posted June 17, 2012 In most modern programming languages, any non-zero value that can be converted to an integer will be true. But I still do not see how "TRUE" can be logically equal to "999" ?! (This I can see... TRUE==1) True is logically equal to all non-zero values. 23.987. 999. "Hello". All of those will be equal to true. I knew that was so in an IF condition, but I did not know that if you put any non-zero value in a logical comparison that it would evaluate to TRUE. Interesting. Thanks for the info (and being civil) KevinM1... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354713 Share on other sites More sharing options...
Pikachu2000 Posted June 17, 2012 Share Posted June 17, 2012 I really don't give a red rat's ass what NetBeans says. Why don't you show us where you assign the value to $preview? Get you Old-Man Glasses out and maybe you'll see that I posted my code in Reply #10... Debbie So then if you knew you assigned the boolean value of TRUE to the $preview variable, why did you continue to insist that you had given it the integer value of 1? I said NetBeans showed "1" and you guys are saying "TRUE" is the same as "1" Alzheimer's disease or just plain ignorance? Nah, I just like to give you stuff to bitch about... Freebie from me for the curmudgeon... Debbie So ignorance then. Got it. Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354715 Share on other sites More sharing options...
kicken Posted June 17, 2012 Share Posted June 17, 2012 I knew that was so in an IF condition, but I did not know that if you put any non-zero value in a logical comparison that it would evaluate to TRUE. It has to do with how PHP decides to interpret the types for the comparison. Since your testing a boolean==integer, php decides to cast the integer to a boolean and evaluate the condition as boolean==boolean. so: $preview == 999 becomes TRUE == (boolean)999 which becomes TRUE == TRUE. You can force php to interpret something a specific way by doing your own casting, such as: if ((int)$preview == 999): which would force php to cast $preview to an int (1) then evaluate 1 == 999, which would be false. Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354716 Share on other sites More sharing options...
Andy-H Posted June 17, 2012 Share Posted June 17, 2012 Think about it like this, behind the scenes php sees that you're using a boolean value and comparing it to an integer, so it casts the integer value to a boolean value for you, if you used the strict (typed) comparison operator, it would evaluate to false: var_dump( true === 999 ); //are true and 999 equal in both type and value? (false) var_dump( true == 999 ); //are true and 999 equal in value (true) //why? var_dump((bool)999); // true var_dump(true == true); // true Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354717 Share on other sites More sharing options...
PFMaBiSmAd Posted June 17, 2012 Share Posted June 17, 2012 http://us3.php.net/manual/en/types.comparisons.php Quote Link to comment https://forums.phpfreaks.com/topic/264349-why-is-this-evalutaing-to-true/#findComment-1354718 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.