php if statement multiple conditions

php if statement multiple conditions
php if statement multiple conditions 

How To Use if-else-if statement PHP with example

If statement in PHP: It is used to test the condition and the result is based on the condition. 

PHP if statement allows conditional execution of code. It is executed if the condition is true.

If the statement is used to execute the block of code that exists inside the if statement only if the

specified condition is true.

If the condition is true its body will execute otherwise does not execute. PHP if-else statement is used to test conditions. There are various ways to use if statements in PHP.

if

if-else

if-else-if

nested if

Important Same Others If Else Statement

👉C If-Else Statement 


👉Java If-Else Statement 


👉Python If-Else Statement


How To Use if Statement in PHP?

<?php

$x=2;

if($x<10)

{

echo "x is less than 10<br>";

}

echo "The value of x is $x";

?>

*****OUTPUT*****

x is less than 10

The value of x is 2


How To Show Result According To Percent?

<?php

$marks=55;

if($marks>=60)

{

echo "First division<br>";

}

if($marks<60 && $marks>=45)

{

echo "Second division<br>";

}

if($marks<45 && $marks>=33)

{

echo "Third division<br>";

}

if($marks<33)

{

echo "Sorry!! You are Fail!!<br>";

}

?>

*****OUTPUT*****

Second division


If else statement in PHP: It is used to test the condition and gives the output in both situations either condition true or false.

PHP if-else statement is executed whether the condition is true or false.

If-else statement is slightly different from the if statement. It executes one block of code if the

specified condition is true and another block of code if the condition is false.

If the condition is the true body of if will execute otherwise body of else execute.

How To Use if-Else Statement in PHP?

<?php

$x=10;

if($x>5)

{

echo "$x is greater than 5";

}

else

{

 echo "$x is less than 5";

}

?>

*****OUTPUT*****

10 is greater than 5


How To Use Voting Program in PHP?

<?php

$age=35;

if($age>5)

{

echo "You are eligible for voting";

}

else

{

echo "You are not eligible for voting";

}

?>

*****OUTPUT*****

You are eligible for voting


How To Use Login Program Using If-Else in PHP?

<?php

if(isset($_POST['btn']))

{

$un

$un=$_POST['u'];

$pwd

$pwd=$_POST['p'];

if($un=="icoderweb" && $pwd=="12345")

{

echo "Login success";

}

else

{

echo "Login failed";

}

}

?>

<form method="post" enctype="multipart/form-data">

<table border="0" width="15%" cellpadding="5px" style="border:2px solid red"> <tr>

<td>Username</td>

<td><input type="text" name="u" />

</td>

</tr>

<tr>

<td>Password</td>

<td><input type="password" name="p" /></td>

</tr>

 <tr><td></td>

<td><input type="submit" name="btn" value="Login" /></td></tr>

 </table>

 </form>

*****OUTPUT*****


If else if Ladder Statement in PHP: It is used to test the condition. If executes only one condition at a time. 
The PHP if-else-if is a special statement used to combine multiple if?.else statements. So, we can check multiple conditions using this statement.

The condition which is true first from the top will execute.

How To Use if-else-if-ladder Statement in PHP?

<?php

$x=10;

if($x<5)

{

echo "Hello1";

}

else if($x>5)

{

echo "Hello2";

}

else if($x<15)

{

echo "Hello3";

}

else

{

echo "Hello4";

}

?>

*****OUTPUT*****

Hello2


How To Show Result According To Percent in PHP?

<?php

$marks=55;

if($marks>=60)

{

echo "First division<br>";

}

if($marks<60 && $marks>=45)

{

echo "Second division<br>";

}

if($marks<45 && $marks>=33)

{

echo "Third division<br>";

}

if($marks<33)

{

echo "Sorry!! You are Fail!!<br>";

}

?>

*****OUTPUT*****

Second division


Nested if statement in PHP: It is used to test the condition. One if inside another if is called nested if. 
The nested if statement contains the if block inside another if block. The inner if statement executes only when a specified condition in the outer if statement is true.


How To Use Nested Statement in PHP?

<?php

$no=5;

if($no>=2)

{

if($no<3) {

 echo "Hello1<br>";

}

echo "Hello2";

}

?>

*****OUTPUT*****

Hello2


How To Use Nested Statement in PHP?

<?php

$no=5;

if($no>=2)

{

if($no>3)

{

echo "Hello1<br>";

}

echo "Hello2";

}

?>

*****OUTPUT*****

Hello1

Hello2


How To Use Nested Statement in PHP?

<?php

$no=5;

if($no<2)

{

if($no>3)

{

echo "Hello1<br>";

}

echo "Hello2";

}

?>

*****OUTPUT*****

no output because first condition(outside if) is false

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.