Xeoncross Posted June 23, 2008 Share Posted June 23, 2008 I am trying to figure out if a item is a STRICT true/false value (and not if an item EQUATES to true or false). I want to allow items that are: true 'true' 1 false 'false' 0 without allowing items that act as true/false values. (i.e. 'stringname' == true while null == false) Now I can do something clunky like: <?php if($value === 'true' || $value === true || $value === 1 || $value === 'false' || $value === false || $value === 0) { print 'value is a plain true/false value'; } else { print 'value is something else'; } ?> But who wants to do something like that? Take the following code that doesn't quite work: <?php $myarray = array( 'true', true, 1, 'false', false, 0, 'string', '', " " ); foreach($myarray as $value) { print ((boolean)$value && $value !== 'false') ? 'true' : 'false'; print "<br />\n"; } ?> Which outputs : true true true false false false true false true I want to get the first 6 to equate to TRUE while getting the last 3 to equate to FALSE. I don't care if the value is true or false - I just want to know IF it is true or false (boolean style). Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/111428-checking-truefalse-boolean-values-not-equates/ Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Just use == and not ===. Quote Link to comment https://forums.phpfreaks.com/topic/111428-checking-truefalse-boolean-values-not-equates/#findComment-571966 Share on other sites More sharing options...
Xeoncross Posted June 23, 2008 Author Share Posted June 23, 2008 Just use == and not ===. You didn't read the post. null == false (WRONG) '' == false (WRONG) 'string_true' == true (WRONG) array('true') == true (WRONG) Quote Link to comment https://forums.phpfreaks.com/topic/111428-checking-truefalse-boolean-values-not-equates/#findComment-571970 Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Okay, then use === and not ==. O_O Quote Link to comment https://forums.phpfreaks.com/topic/111428-checking-truefalse-boolean-values-not-equates/#findComment-571976 Share on other sites More sharing options...
Xeoncross Posted June 23, 2008 Author Share Posted June 23, 2008 Okay, then use === and not ==. O_O This isn't as simple as you seem to think it is. Either I didn't explain the problem like I should - or it is over your head. Please re-read the whole thing and you will see I am looking for values that represent strict boolean values - not values that that are true or false. Quote Link to comment https://forums.phpfreaks.com/topic/111428-checking-truefalse-boolean-values-not-equates/#findComment-571988 Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 I highly doubt it's over my head. Explain it again. I don't understand at all what you're actually asking. === will work with strict booleans, not 0, NULL, 'lol', or anything. if ($bool === true) { echo "Boolean true."; } else { echo "Not boolean true."; } Quote Link to comment https://forums.phpfreaks.com/topic/111428-checking-truefalse-boolean-values-not-equates/#findComment-571992 Share on other sites More sharing options...
Xeoncross Posted June 23, 2008 Author Share Posted June 23, 2008 This is NOT what I want to do as it will allow any value that is TRUE (not just 'true', true, and 1) <?php if ($bool === true) { echo "Boolean true."; } else { echo "Not boolean true."; } ?> I only want to allow values that are === true || 'true' || 1 || false || 'false' || 0 <?php if($value === 'true' || $value === true || $value === 1 || $value === 'false' || $value === false || $value === 0) { print 'this is a boolean value'; } else { print 'This might be equal to true or false - but it is NOT a boolean value'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/111428-checking-truefalse-boolean-values-not-equates/#findComment-571995 Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 May I ask why? =/ There's no real way to do that other than the way you outlined earlier simply because those are all different values that in a set of other values that could be true. For example, without strict bounds like you showed in the original post, nothing stops "the", 42, or "superhero" from being "true". Quote Link to comment https://forums.phpfreaks.com/topic/111428-checking-truefalse-boolean-values-not-equates/#findComment-571996 Share on other sites More sharing options...
kenrbnsn Posted June 23, 2008 Share Posted June 23, 2008 Does the following do what you're looking for? <?php function really_true_false($val) { $ret = false; switch (true) { case ($val === true): case ($val === 'true'): case ($val === 1); case ($val === false): case ($val === 'false'); case ($val === 0); $ret = true; break; } return ($ret); } $myarray = array( 'true', true, 1, 'false', false, 0, 'string', '', " " ); foreach ($myarray as $tst) echo (really_true_false($tst))?'true<br>':'false<br>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/111428-checking-truefalse-boolean-values-not-equates/#findComment-572024 Share on other sites More sharing options...
Xeoncross Posted June 23, 2008 Author Share Posted June 23, 2008 May I ask why? =/ When dealing with php ini files the following values do NOT need quotes around them: true 1 false 0 on off However, everything else in a PHP ini file is a string (except sections). Therefore the values "false" and false both need to be treated as a boolean (0) FALSE when working with ini files and not as strings. Quote Link to comment https://forums.phpfreaks.com/topic/111428-checking-truefalse-boolean-values-not-equates/#findComment-572218 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.