For those who have used any computer language, you’ve bound to have run into “if” a few times. It’s the most basic conditional construct there is. If this, do that.

In PHP there are a few ways to write an if statement. This is the most common: if with some statement in parentheses which evaluates to true or false, followed by a set of curly braces which contain the code to be executed should that statement evaluate to true.

if( true ) {
  echo 'good';
}

If you have only one statement of code to execute inside of the curly braces, you can actually do away with the braces. It works but I don’t recommend it. Changes to the code later on may break the intended functionality without anyone noticing by reading the code.

Get into the habit of always using curly braces with your if statements.

// does the same as the above but
// is not recommended
if( true )
  echo 'good';
 
// this will echo "bad" because only the first statement (not line)
// is tied to the condition
if( false )
  echo 'not good'; echo 'bad';

One other way to take that format and change it slightly is to use a colon to start the block, and endif; to close it.

// also does the same
if( true ) :
  echo 'good';
endif;

Some say one is easier to read than the other but that depends on the rest of the code’s structure (indention, line length, etc.)