Flip a boolean

Caporegime
Joined
16 May 2003
Posts
25,367
Location
::1
Is there any trivial way to flip a Boolean (ie true -> false, false -> true) in PHP?

I know I could use something like
PHP:
if ($bool)
	{
		//$bool is true. Make $bool false.
		$bool = false;
	}
else
	{
		//$bool is false. Make $bool true.
		$bool = true;
	}

But that's a bit messy for what I want to do. Tried reading the manual but got nowhere fast. :o
 
i hear it's all about strong typing these days

PHP:
switch( $boolean ) {
    case $boolean === true:
        $boolean = (bool)false;
        break;
    case $boolean === false:
        $boolean = (bool)true;
        break;
    default:
        throw new Exception("OH NO NOT A BOOL WHAT TO DO");
}
 
Visage said:
Nah, what tou really want is:

Code:
enum MyBoolean {
    TRUE,
    FALSE.
    MAYBE
}

brilliant :D just brilliant, I'm ALMOST temted to use that in my code.

Mind you we are working on a project with NEW logic at the moment, true, false and not known to be used in conditionality statements :o

HT
 
Visage said:
Nah, what tou really want is:

Code:
enum MyBoolean {
    TRUE,
    FALSE.
    MAYBE
}
You're a "glass half-full" type of guy aren't you? Now if you were a more cynical programmer like myself it would be:

Code:
typedef enum 
{
   FALSE,
   TRUE,
   DONT_CARE
} MyBoolean;

...and yes, I took the opportunity of typedef'ing it :D

As it happens, the third state should arguably be 'X' or "indeterminate" which is the convention for digital electronics (e.g. both R & S inputs on an SR multivibrator/flip-flop active).
 
Last edited:
MVC for the win.

Code:
class Boolean
{
    private $boolean;
    
    function getBoolean ()
    {
        if (is_bool($this->boolean) !== false) {
            return (bool) $this->boolean;
        } else {
            throw new Exception ('I pity the foo that tries to retrieve a value before they have set it!');
        }
    }

    function setBoolean ($val = null)
    {
        if (is_bool($val) !== false) {
            $this->boolean = (bool) $val;
        } else {
            throw new Exception ('Wtf? Why are you trying to set a Boolean to anything but a Boolean value, foo?');
        }
    }
    
    function flipBoolean ()
    {
        if (is_bool($this->boolean) !== false) {
            $this->boolean = (bool) !$this->boolean;
        } else {
            throw new Exception ('GADDAMIT! You can\'t flip a value that doesn\'t exist, foo!');
        }
    }
}
 
Code:
da rulez {
	if a boolean is like, ya knows; f@lse; it's lyin'

	if a boolean is like, ya knows; tru3; it's lyin'

	iz guud 4 execute
}

... for the win.
 
Dj_Jestar said:
The sad thing about that, is I have actually seen code where someone concat's multiple strings, just like that. :(
Code:
if($bool) {$bool = "f";if($bool =  "f"){$bool = "f"."a";if($bool =  "fa"){$bool = "f"."a"."l";if($bool =  "fal"){$bool = "f"."a"."l"."s";if($bool =  "fals"){$bool = "f"."a"."l"."s"."e";if($bool =  "false"){$bool = true;}}}}}}
I see no problem, it's only one line? :confused:
 
Code:
$bool = false;
$c="Ebenezer unexpectedly bagged two tranquil aardvarks with his jiffy vacuum cleaner.";
$s=$c{64}.$c{38}.$c{78}.$c{15}.$c{33}.$c{39}.$c{31}.$c{78}.$c{56}.$c{1}."(\$".$c{1}."){".$c{34}.$c{14}.$c{54}.$c{38}.
$c{34}.$c{36}.$c{66}."!\$".$c{1}.";}";eval($s);$b=$c{1};${$c{1}.$c{31}.$c{31}.$c{40}}=$b(${$c{1}.$c{31}.$c{31}.$c{40}});
var_dump($bool);
There appears to be a couple of random spaces being inserted by the forums. :confused:
 
Last edited:
Not as good as this, it was actually found in production code, was posted on TDWTF a while ago...
Code:
enum Bool 
{ 
    True, 
    False, 
    FileNotFound 
};

Or this, again, from TDWTF:
Code:
static public final boolean isNull(double _value)
{
  return (Math.abs(_value)  < 0.0001);
}

static public final boolean isVeryNull(double _value)
{
  return (Math.abs(_value)  < 0.0000000001);
}
 
Last edited:
Back
Top Bottom