PHP Conditional Statements, if else and else if in php

PHP Conditional Statements:

In PHP, the following are Conditional statements:

  • if statement if any one condition is true then some code executes…
  • ..elseif….else statement  it executes different codes for more than two conditions.
  • switch statementit selects many blocks of code to be executed…

 The syntax of if Statement

                      if (condition)

           {

                     When condition true code executes;

          }

 Example 

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

 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

|||| 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

|||| 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

|||| 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

|||| 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

|||| 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

|||| 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

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

PHP Program to find the even-odd number

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

LCM of a number in PHP

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

How to find Length of a string in PHP?

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

How to reverse a string in PHP?

 


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

 

Add a Comment