PHP – If statements

Most programming languages provide a conditional statement of some kind. PHP provides an if then else, the syntax of which is very similar to Javascript and other languages. In PHP the if statement has a few extra features.

Let’s start with the basic if statement.

if ( condition ) {
  // code block
}

Here if the condition is  evaluated to true the code block is executed. You can add an alternative code block that will be executed if the condition is evaluated to false.

if ( condition ) {
  // code block true 
} else {
  // code block false
}

Here the the second code block is executed when the condition is false.

Here’s a couple real world examples.

<?php 
  if ( isset($_GET['category']) ) {
    echo $_GET['category'];
  }
?>

In this example if the variable $_GET[‘category’] has been set the value is printed to the page.

We can add an else to this:

<?php 
  if ( isset($_GET['category']) ) {
    echo $_GET['category'];
  } else {
    echo "Category not set";
  }
?>

Here if the $_GET[‘category’] variable is set, then it is printed to the page, otherwise (else) the message “Category not set” is printed instead.

Since PHP is an html embedded language you will often find that you want to print HTML and other text to the page as part of the if and else. Using echo is okay in some cases. Fo example:

<?php 
  if ( isset($_GET['category']) ) {
    echo "<p>" . $_GET['category'] . "</p>";
  } else {
    echo "<p>Category not set</p>";
  }
?>

In other cases the situation gets more complex. For example imagine that you are check if someone is logged in, and if not you want to display a form:

<?php 
  if ( isset($_GET['loggedin']) ) {
    echo "<p>Logged in:" . $_GET['username'] . "</p>";
  } else {
    echo "<form>
<input type='text'>
...
</form>";
  }
?>

Printing the form using echo is awkward. I didn’t write the entire form above, use your imagination, it can get a little ugly; worst of all everything is inside the quotes so you don’t get any color coding or other help from you editor.

In these cases there is a better way. PHP allows you to close PHP and reopen the PHP tag anywhere. Here’s an example:

<?php 
  if ( isset($_GET['loggedin']) ) {
    echo "<p>Logged in:" . $_GET['username'] . "</p>";
  } else {
    ?>
<form>
<input type='text' name='username'>
<input type='password' name='password'>
<input type='submit' name='submit'> 
</form>
  <?php } ?>

Here we exit PHP after the else statement, so the form can be written as standard HTML. Not that that the if statement has the it’s complete syntax within the <?php ?> tags. The lone } looks a little odd inside of <?php ?> on the last, but this completes the syntax so everything is okay.

Alternately you could write the same statement above with this alternate syntax.

<?php 
  if ( isset($_GET['loggedin']) ) :
    echo "<p>Logged in:" . $_GET['username'] . "</p>";
  else:
    ?>
<form>
<input type='text' name='username'>
<input type='password' name='password'>
<input type='submit' name='submit'> 
</form>
  <?php endif; ?>

Leave a Reply

Your email address will not be published. Required fields are marked *