PHP boolean values – shorter and semantic IFs

by Carey on November 2, 2010

Post image for PHP boolean values – shorter and semantic IFs
  • Share
  • CevherShare
  • Share

This is a simple one but I am all for making code neater, tidier and more readable.

If you are using a boolean value for evaluating an IF statement, you would typically:

$valid = true;

if ($valid == true)
{
# do something
}

else

{
# do something else

}

A shorter and more readable (try saying it out loud in pseudo code) method would be:

 if ($valid)
{
# do something
} 

And to test for false:

 if (!$valid)
{
# do something
} 

A basic one, but I find the little touches make a big difference.

Leave a Comment

Previous post:

Next post: