PHP if Statement

Hey Guys,
In this PHP tutorial, we are going to discuss if statement in PHP. And how we will use this statement in PHP. Before going to know about if statement lets discuss conditional statements.
Conditional statements are of 3 types if, else and elif. Now, we are discussing if statement.

PHP if Statement

If is a conditional statement which is used in all major programming languages.The if statement in PHP used when we deal with the conditions.

It is similar to our general English If.

Here we execute the code if the condition is true otherwise not.

And it is very simple but one of the most used syntax in programming.

Whenever we deal with conditions we use if and if can also be used multiple times known as nested if statements.

If we have one ‘if’ statement inside of the other ‘if’ then we call it as nested if.

example of if statement in PHP

<?php

$a=2;

if ($a>1)

echo "hello";

?>

Output

hello

here the statement hello is printed as the condition (a>1) is true. And if the condition gets fail it wont be executed.

example of nested if statements in PHP

<?php


$a=2;

$b=3;

if ($a>1)

  if($b>1)

    echo "hello hi";

?>

Output

hello hi

Here the statement hello hi is printed when both the statements a>1 and b>1 are true.

If any single statement gets false then the statement didn’t get printed.

Here, first condition one is checked and then if it is true we check the second condition else we would quit the operation.

If the second is true after the first condition is true then we execute the next code and print the statement hello hi.

You can also read:

Echo and Print Statement Difference in PHP

Leave a Reply

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