Site icon T4Tutorials.com

PHP Conditional Statements, if else and else if in php

PHP Conditional Statements:

In PHP, the following are Conditional statements:

 The syntax of if Statement

                      if (condition)

           {

                     When condition true code executes;

          }

 Example 

<?php
$S = date("H");
if ($S < "12") {
  echo "welcome to t4tutorials!";
}
?>

Output

welcome to t4tutorials

Explanation of code

Code  Comments
<?php Starting php
$S = date(“D”); assigning function date value to variable
if ($S < “15”) checking the condition
 echo print the text
?> Closing of php

   

   if…else Statement

If the condition is True then it executes the True part of the statements, otherwise, it will execute else part of the statements.

   Syntax of if…else Statement

    if (condition)

    {
    When condition true code executes;
    }

      else

    {
   When condition false code executes;
    }

   Example of if…else Statement

<?php
$t = date("H");

if ($t < "10") {
  echo "Have a good morning!";
} elseif ($t < "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}
?>

 if…else if….else Statement

Here we will check multiple conditions to get them True, If none of the conditions is True, then else part will execute.

      Syntax of if…else if….else Statement

      if (condition)

       {
     When condition true code executes;
        }

      else if (condition)

       {
      When condition true code executes;
        }

      else

        {

      When all conditions false code executes;
        }

Examples of if-else Conditional statements in PHP

PHP Program To Find The Length Of A String using if and built-in function

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form method="post">
<input type="text" name="name"/>
<input type="submit" name="OK" />
</form>
<?php
if(isset($_POST['OK'])){
$str = $_POST['name'];
print strlen($str); 
}
?>
</body>
</html>

|||| Click to read Complex programs of ‘Finding The Length Of A String” (not for beginners) ||||

PHP program to convert Fahrenheit to Celsius by asking the user to input a number

<html>
<head>
<meta charset="utf-8">
<strong><h1>Convert fahrenheit to Celcius</h1></strong>
</head>
<body align="center">
<center>
<form method="post">
<br><br><br>
 Enter Temprature in Fahrenheit : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="far">
<br><br>
<input type="submit" name= "submit" value="Convert" style="font-size: 2em; background-color:  #f27669;">
<input type="reset" value="Reset" style="font-size: 2em; background-color: #4c4c4c;">
   </center>

<?php
if(isset($_POST['submit']))
{
	$f= $_POST['far'];
	$c= ($f - 32) * (5/9);
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style='background-color: #e4ddcb'><center><label class='col-sm-2 control-label' >Temprature in Celcius =</label> <input class='easypositive' value=$c ></span></center>";
}		
	?>
</body>
</html>

|||| Click to read Complex programs of ‘“converting Fahrenheit to Celsius” (not for beginners) ||||

PHP program to swap the two numbers by asking the user to input a number

<?php
$n1="";
$n2="";
$n3="";
$r1="";
$r2="";
if(isset($_POST["S1"]))
{
	$n1=$_POST["T1"];
	$n2=$_POST["T2"];
	
	
	$r1=$n2;
	$r2=$n1;
	
}

?>
<html>
<body>
    <h2 align="center">Swap two numbers using a third variable</h2>

    <form method="post" name="Form1" >


        <table align="center" >
            <tr><td >Value of A</td>
				<td>= <input  name="T1" size="15"  type="text" value="<?php echo($n1);?>"></td>
			</tr>
			
            <tr>
				<td >Value of B</td><td>= <input  name="T2" size="15"    type ="text" value="<?php echo($n2);?>"> </td>
			</tr>
			            	
			<tr><td colspan="2"><p></p></td> </tr>
            
			<tr>
				<td colspan="2" align="center"> <input value="SWAP"   type="submit" name="S1"> 
						 </td>
			</tr>
			
			<tr><td colspan="2"><p></p></td> </tr>
			
			<tr><td><h2 align="center">After Swaping</h2></td></tr>
			
            <tr>
				<td >Value of A</td>
				<td>= <input  name="T4" size="15" readonly="" type="text" value="<?php echo($r1);?>"></td>
			</tr>
			
            <tr>
				<td >Value of B</td>
				<td>= <input  name="T5" size="15" readonly="" type="text" value="<?php echo($r2);?>"></td>
			</tr>
			
        </table>

    </form>

</body>
</html>

|||| Click to read Complex programs of ‘swap the two numbers” (not for beginners) ||||

PHP Program to find the area of a rectangle by asking the user to input a number

<form method="POST">
Enter the length
<input type = "text" name = "le">
<br/>

Enter the breadth
<input type = "text" name = "br">
<br/>

<input type= "submit" value = "submit" name="btnsubmit">
</form>
<?php
if(isset($_POST['btnsubmit']))
{
$l=$_POST['le'];
$b=$_POST['br'];

$area=$l*$b;
$peri= 2*($l+$b);
echo "The aea of rectangle ".$area."<br/>";
echo "The perimeter of rectangle ".$peri."<br/>";
}
?>

|||| Click to read Complex programs of ‘“area of a rectangle” (not for beginners) ||||

PHP program to convert a decimal number into binary by asking the user to input a number

<html>
<head>
<title></title>
   
</head>
<body>
                     <div class="nukr"><h2 class="col_insert" >Reverse Number</h2></div>
                     <form action="" method="post" name="insert_form" id="insert_form" class="comment-form">
                     <table class="tableI1" align="left" border="0" bordercolor="#FFFFFF" >
                     <tr><td ><b><i>Enter Number</b></i></td>
                     <td><input id="number" name="number" type="text" size="30" /> 
                     <tr><td></td><td><input type="submit" id="btnsubmit" name="btnsubmit" value="Calculate" size="30"/></td></tr>
                     <tr><td></td><td>       </td></tr>
                     </table>
                      </form>

</body>
</html>
<?php
if(isset($_POST['btnsubmit']))
{

 $number ;$mor=0;
$number=$_POST['number'];
echo"Dacimal Number is = $number<br>";
$mor=decbin($number);

echo"Binary Number is = $mor";

}
?>

|||| Click to read Complex programs of convert a decimal number into binary (not for beginners) ||||

PHP program to find the cube root of a number by asking the user to input a number

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Cube Root</title>
</head>
<body>
<form method="post">
Enter the number  <input type="text" name="Number" /><br />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST['submit']))
{
$num=$_POST['Number'];
$cube_root=(pow($num,(1.0/3.0)));
echo "$cube_root";
}
?>
</body>
</html>

|||| Click to read Complex programs of cube root of a number (not for beginners) ||||

Square Root of A Number in PHP by asking the user to input a number

<html>
<body>
<form method="post">
<p>Enter any number</p>
<input type="text" name="number" />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST['submit'])){
$number=$_POST['number'];
$sr=sqrt($number);

echo"Square root of $number is $sr</h2>";
}
?>
</body>
</html>

|||| Click to read Complex programs of Square Root of A Number (not for beginners) ||||

PHP Program to find the even-odd number

<?php
$a=4;
if($a%2==0)
{
echo $a ." is Even number";
}
else
{
echo $a . " is odd number";
}
echo "<title>Even | Odd</title>";

?>

|||| Click to read Complex programs of even-odd number  (not for beginners) ||||

LCM of a number in PHP

<html>
<body>
<form method="POST"> 
Enter  First No. <input type="text" name="num1">
Enter Second NO.<input type="text" name="num2"><br><br> 
Result: <input type="submit" name="Ok">
</form>
</body>
</html>
<?php 
if (isset($_POST['Ok'])) {
  $num1=$_POST['num1'];
  $num2=$_POST['num2'];
  $max=($num1>$num2) ? $num1 : $num2;
    while(1)
    {
      if($max%$num1==0 && $max%$num2==0)
        {
          echo "LCM of " .$num1. " and " .$num2. " is: ".$max;
          break;
        }
    $max=$max+1;
    }
    }
     //echo '$max';

?>

|||| Click to read Complex programs of LCM of a number in PHP  (not for beginners) ||||

How to find Length of a string in PHP?

<html>
<head><title>Reverse String</title></head>
<body>
<form method="post">
Enter String:
<input type="text" name="str"/>
<input type="submit" name="ok"/>
</form>
<?php
if(isset($_POST['ok'])){
$data = $_POST['str'];
echo" <h3>$data</h3> <br/>";
foreach (count_chars($data, 1) as $i => $val) {
   echo "There were $val  \"" , chr($i) , "\" in the string.<br/>";
}
}
?>
</body>
</html>

|||| Click to read Complex programs of Length of a string  (not for beginners) ||||

How to reverse a string in PHP?

 

<?php
function rev_str($str)
{
 $n = strlen($str);
 if($n == 1)
   {
    return $str;
   }
 else
   {
   $n--;
   return rev_str(substr($str,1, $n)) . substr($str, 0, 1);
   }
}
print_r(rev_str('T4Tutorials.com')."\n");
?>

|||| Click to read Complex programs of reverse a string (not for beginners) ||||

 

Exit mobile version