PHP
lets programmers evaluate different conditions during of a program and take
decisions based on whether these conditions evaluate to true of false. Conditional
statements are used to perform different actions based on different conditions.
PHP support different types of conditional statements
PHP support different types of conditional statements
- If statement
- If-else statement
- If-elseif-else statement
- Switch statement
1. IF Statement – If statement is the
simple condition statement. This statement Output will provide when condition
must be true.
Syntax –
if (condition) {
code to be
executed if condition is true;
}
Example – Write a program to check odd number.
Here number entered by user.
<!DOCTYPE html>
<html>
<head>
<title>Check
Odd</title>
</head>
<body>
<?php
if(isset($_POST['submitbtn']))
{
if($_POST['num']%2
!= 0)
{
echo
"This number is Odd";
}
}
?>
<form method="post">
<input
type="text" name="num" placeholder="Enter number
...">
<input
type="submit" name="submitbtn" value="Check
Odd">
</form>
</body>
</html>
Example
– Write a program to check number is Positive. Here number entered by user.
<!DOCTYPE html>
<html>
<head>
<title>Check
P N</title>
</head>
<body>
<?php
if(isset($_POST['submitbtn']))
{
if
($_POST['number'] > 0) {
echo
"Number is positive.";
}
}
?>
<form
method="post">
<input
type="text" name="number" placeholder="Enter number
...">
<input
type="submit" name="submitbtn" value="Check No">
</form>
</body>
</html>
2. If else statement - If-else
statements allows you to display output in both the condition. If condition is
true display some message otherwise display other message.
Syntax –
if(Condition)
{
Code executed if condition is true;
}
else
{
Code executed if condition is false;
}
Example – Write a program to check number is positive or
negative. Here number entered by user.
<!DOCTYPE html>
<html>
<head>
<title>Check
P N</title>
</head>
<body>
<?php
if(isset($_POST['submitbtn']))
{
if
($_POST['number'] > 0) {
echo
"Number is positive.";
}
else
{
echo
"Number is negative";
}
}
?>
<form
method="post">
<input
type="text" name="number" placeholder="Enter number
...">
<input
type="submit" name="submitbtn" value="Check No">
</form>
</body>
</html>
3. If elseif else Statement - If you want
to execute some code if one of the several conditions are true use the elseif
statement. If elseif else statement executes different codes for more than two
conditions.
Syntax
–
If(Condition)
{
Code executed if condition is true;
}
elseif
{
Code executed if condition is true;
}
else
{
Code executed if all condition is false;
}
Example
– Write a program check given number is Positive, Negative or Zero.
<!DOCTYPE html>
<html>
<head>
<title>Check
P N</title>
</head>
<body>
<?php
if(isset($_POST['submitbtn']))
{
if
($_POST['number'] > 0) {
echo
"Number is positive.";
}
elseif($_POST['number']
< 0)
{
echo
"Number is negative";
}
else
{
echo
"Number is Zero";
}
}
?>
<form
method="post">
<input
type="text" name="number" placeholder="Enter number
...">
<input
type="submit" name="submitbtn" value="Check No">
</form>
</body>
</html>
4. Switch Statement - If you want
to select one of many blocks of code to be executed, use the Switch statement. The
switch statement is used to perform different actions based on different
conditions.
Syntax –
switch(number)
{
case label1:
Code executed if number = label1;
break;
case label2:
Code executed if number = label2;
break;
default:
Code executed if number is different from
all labels;
}
Example – Write an program print related day name
according to inputted number (1-7) using switch condition.
<!DOCTYPE html>
<html>
<head>
<title>Switch</title>
</head>
<body>
<?php
if(isset($_POST['submitbtn']))
{
if($_POST['number'])
{
switch
($_POST['number']) {
case
'1':
echo
"Sunday";
break;
case
'2':
echo
"Monday";
break;
case
'3':
echo
"Tuesday";
break;
case
'4':
echo
"Wednesday";
break;
case
'5':
echo
"Thursday";
break;
case
'6':
echo
"Friday";
break;
case
'7':
echo
"Saturday";
break;
default:
echo
"Wrong Choise";
break;
}
}
}
?>
<form
method="post">
<input
type="text" name="number" placeholder="Enter number
...">
<input
type="submit" name="submitbtn" value="Check">
</form>
</body>
</html>
0 Comments