PHP 7.x — P24: If Statement

Dino Cajic
Jul 23, 2020 · 3 min read
Image for post
Image for post

The if statement is probably the first piece of code that you wrote. “If” statements are conditional statements, meaning that if the expression that they’re evaluating is true, the if statement will allow for the execution of the expressions inside of the if statement body.

The anatomy of the if statement looks like this:

<?phpif ( boolean expression ) {
expressions in body
}
?>

The if statement starts with the word if, followed by an expression enclosed by parentheses. The expression will evaluate to either true or false. If the expression evaluates to true, the expressions inside the if statement body (normally surrounded by curly braces) will be executed.

Let’s look at a quick example. We are going to guarantee that the expression inside the if statement evaluates to true by passing the boolean value true to it.

<?phpif ( true ) {
echo "Donkey";
}
?>

The code above will output Donkey since the boolean expression inside the if statement evaluates to true. Any type of expression that evaluates to true or false can be placed inside the conditional.

<?php$a = 10;if ( $a > 5 ) {
echo "Math";
}
?>

PHP evaluates the if statement above as follows:

  1. PHP sees the keyword if.
  2. It looks for the opening parentheses.
  3. It evaluates the expression inside the parentheses. Is the value stored in $a greater than 5? In other words, is 10 greater than 5? Yes. So, the expression is true.
  4. Since the expression is true, PHP looks inside of the if statement body and finds an echo statement. It echoes out Math.

You can also compare strings inside of the boolean expression. As was mentioned earlier, the expression just needs to evaluate to true or false.

<?php$word = "confused";if ( $word == "confused" ){
echo "Bumfuzzle";
}
?>

You can also use the negation operator to flip the value of the conditional statement. With the negation operator, if the value is true, the negated value will be false; if the value is false, the negated value will be true.

When would you need to use the negation operator? Sometimes you need to evaluate the expression inside of the if statement when the conditional evaluates to false. In that case, you can append the ! operator and flip the false to true. Remember, the only way for the statements to get executed inside of the if statement is if the condition evaluates to true.

<?php$definitelyfalse = false;if ( !$definitelyfalse ) { 
echo "Opposite of false is true";
}
?>

The if statement body can contain multiple expressions.

<?phpif ( true ) {
$a = 3;
$b = 5;
$c = $a + $b;
echo $c;}?>

If there is only one expression inside of the if-statement body, curly braces may be omitted.

<?php$oneExpression = true;if ( $oneExpression)
echo "One Expression";
?>

Curly braces don’t have to be used even with multiple expressions. PHP offers alternate syntax with colon/endif.

<?php$time = 6;if ( $time >= 6 && $time < 9 ):
echo "Early Morning";
endif;
?>

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Dino Cajic

Written by

Author of An Illustrative Introduction to Algorithms. A Software Engineer with a B.S. in Computer Science, a minor in Biology, and a passion for learning.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Dino Cajic

Written by

Author of An Illustrative Introduction to Algorithms. A Software Engineer with a B.S. in Computer Science, a minor in Biology, and a passion for learning.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store