Take the 2-minute tour ×

Let's say I have the following function which should return True if valid and False if invalid.

Is there a shortcut to return the 'opposite' of the boolean $this->errors?

Should I use a one-line if statement or is there another possibilty?

So if $this->errors is false I want it to return true :P

function valid() {

    $this->errors = False;

    if ($somethingiswrong) {
        $this->errors = True;

    return $this->errors;


OMG How come I never see the right questions/answers when I use the search option. But find the right answers when I already posted :P

return !$this->errors;

Is what I was looking for.

share|improve this question
return $this->errors ? false : true; JUST KIDDING don't do this ;) –  Byron Whitlock Apr 29 '11 at 22:00
return ($this->errors != false ? false : true) just to make things even more confusing for the next person to stumble into that code block –  Marc B Apr 29 '11 at 22:04
@Byron and Marc: I might just do that just to screw with people ;) –  PeeHaa Apr 29 '11 at 22:06
What the heck it's Friday: return !!!!!(!!!$this->errors ? !!(boolean)false : !!(boolean)'false'); –  webbiedave Apr 29 '11 at 22:19
@webbiedave: omg lol you should really stop using meth and/or crack :P –  PeeHaa Apr 29 '11 at 22:27
add comment

1 Answer

up vote 15 down vote accepted
return !$this->errors;

! is logical negation.

share|improve this answer
Thanks! :D That's it as I also found at the related section. However thanks again! –  PeeHaa Apr 29 '11 at 21:59
add comment

Your Answer


