PHP if statements

An introduction to PHP if statements, the syntax, how to use them and some examples

In this PHP tutorial I hope to introduce you to PHP if statements, how they work and how they can be used. I expect you to already have some (even if limited) PHP programming knowledge before reading this tutorial, that is to say this is should NOT be the first PHP tutorial you read. So without much else, let's start...

Syntax

A PHP if statement compares two or more "things" - variables, constants, numbers, strings, settings - all sorts. The basic styntax is as follows:

<?php
if(expression)
{
// do something
}
?>

We start off with an 'if' keyword followed by a pair of round brackets which contains the expression - if this bit evaluates to TRUE the content in the following curly brackets is executed. If the expression is evaluates to FALSE it does not.

The basics

The best place to start is by comparing two items. This can be demonstrated with two basic examples, firstly by comparing a variable to a value:

<?php
$a 
1;
if(
$a == 1)
{
  echo 
'1 is equal to 1';
}
?>

This will output:

1 is equal to 1

Because the variable $a is 1 which is equal to 1! Or we could compare two variables...

<?php
$a 
1;
$b 2;
if(
$a == $b)
{
  echo 
'1 is equal to 2';
}
?>

Will output:

 

.. because $a is 1 which of course doesn't equal the value of $b, 2, so the code is ignored.

Remember that a double equals (==) is used to check if two things are equal in PHP as a single equals is used to assign values.

Triple equals

At times you may come across an if statement which uses 3 equals signs to compare two things. This is used to not only check the values but also the data types of the two items. Again, this can be demonstrated with two examples:

<?php
$a 
1;
if(
$a === 1)
{
  echo 
'1 is equal to 1';
}
?>

This will output:

1 is equal to 1

Because the variable $a is an integer with the value of 1 as is the 1 to the right of the triple equals.

<?php
$a 
1;
if(
$a === '1')
{
  echo 
'1 is equal to 1';
}
?>

Will output:

 

... because although both variables have the same value the data type this time to the right of the triple equals is a string (denoted by the single quote marks). As the two data types are different the code is ignored.

A step further

So now you should be able to check if two items are equal using PHP - but how else can be compare two items? In this section I'll explain how to compare items in 5 more ways

The NOT operator

You may want to check if two items are not equal - this is done by using a ! before the equals signs. Here is an example:

<?php
$a 
1;
if(
$a != 2)
{
  echo 
'1 is not equal to 2';
}
?>

This will output:

1 is not equal to 2

Notice when we use the not operator that we also only use a single equals sign

Greater than and greater than or equal to

The next two operators are greater than: > and greater than or equal to: >=. Again, here are some examples to demonstrate the use of these operators in an if statement, firstly greater than:

<?php
$a 
1;
if(
$a 0)
{
  echo 
'1 is greater than 0';
}
?>

This will output:

1 is greater than 0

Greater than or equal to will also check if the two items are equal as well as if the one on the left of the operator is greater than the other:

<?php
$a 
1;
if(
$a >= 1)
{
  echo 
'1 is greater than or equal to 0';
}
?>

This will output:

1 is greater than or equal to 0

Less than and less than or equal to

The next two operators are less than: < and less than or equal to: <=. These are the exact opposite as the previous two operators. Here are some examples to demonstrate the use of these operators in an if statement, firstly less than:

<?php
$a 
1;
if(
$a 0)
{
  echo 
'1 is less than 0';
}
?>

This will output:

 

Nothing, because 1 isn't less than 0. Less than or equal to will also check if the two items are equal as well as if the one on the left of the operator is less than the other:

<?php
$a 
1;
if(
$a <= 1)
{
  echo 
'1 is less than or equal to 0';
}
?>

This will output:

1 is less than or equal to 0

And that concludes this introduction to PHP if statements.

 

Return to top Bookmark with:


advertisement

Tips & advice

Need coding help? Tips, tricks or advice? Check out our webmaster forums