IF - PHP If and else Statement


 

IF - PHP If and else Statement

PHP IF Statement is a conditional statement that gives the opportunity to choose between two values. It can also be used to validate the program.

PHP IF Statement is a conditional statement that gives the opportunity to choose between two values. It can also be used to validate the program.

IF - PHP If and else Statement in PHP

“IF” is a conditional statement that gives the opportunity to choose between two values. It can also be used to validate the program. Login applications are good example of if else statement. 

Apart from PHP there are many more programming languages that too uses if else statement as a conditional operator.

Lets take an example to understand it in better way... suppose we have only one item in the database and if the user chooses the same item he can shop else a message will given to user that “there is no such item available”.

Code:

<?php
$item=1;
if ($item==1)
  echo "You can buy!";
else
  echo "There is no such item available.";
?>

This was just a simple example that has used if statement only one time in the whole code. But there could be situation like you wanted to use it more then once. PHP also support this feature, you can use if statement in your program as many times as you want.

Syntax of PHP if else if Statement

if (condition)
  Execute if condition is true;

elseif (condition)
Executed if second condition is true;

else
In case both conditions are false;

Ads