Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

PHP nested if else statement tutorial with working example

PHP nested if else statement tutorial with working example

In the following example I am going to show how to use PHP nested if else statement with working examples,  examples also demonstrate how to use Use of logical AND(&&) and OR(||) oprator in if else statement.
Question:  write a program to enter age and gender of a user and check marital status of user according to Indian marriage rule.
Solution are here :
pro1.php
<?php
$age=$_POST[‘age’];
$gen=$_POST[‘gen’];
if(isset($_POST[‘b1′]))
{
if($age>=21)
$msg=”Eligible”;
else
{
if($age>=18)
{
if($gen==’f’||$gen==’F’)
$msg=’Eligible’;
else
$msg=’Not eligible’;
}
else
$msg=’Not Eligible’;
}
}
?>
<html>
<head><title>form</title></head>
<body>
<form action=”” method=”post”>
Enter your Age<input type=”text” name=”age”><br>
Enter your Gender(m/f)<input type=”text” name=”gen”><br>
Status<input type=”text” value=”<?php echo $msg;?>”><br>
<input type=”submit” name=”b1″ value=”Check”>
</form>
</body>
</html>Use of logical AND(&&) and OR(||)oprator:
Now consider the program, the code complexity of pro1.php is shorted using logical &&  operator in pro2.php so save and run this program.
Pro2.php
<?php
$age=$_POST[‘age’];
$gen=$_POST[‘gen’];
if(isset($_POST[‘b1′]))
{
if($age>=21)
$msg=”Eligible”;
else
{
if(($age>=18)&&($gen==’f’||$gen==’F’))
$msg=’Eligible’;
else
$msg=’Not eligible’;
}
}
?>
<html>
<head><title>form</title></head>
<body>
<form action=”” method=”post”>
Enter your Age<input type=”text” name=”age”><br>
Select Gender<input type=”radio” name=”gen” value=”m”>Male
<input type=”radio” name=”gen” value=”f”>Female<br>
Status<input type=”text” value=”<?php echo $msg;?>”><br>
<input type=”submit” name=”b1″ value=”Check”>
</form>
</body>
</html>

3 Comment

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do you have any Query / Suggestion