Jump to content

Recommended Posts

 

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?  ;D

 

 

 

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.

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.";

}

 

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';
}

?>

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". 

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.