Interview Questions

PHP interview questions
Web UI developer
Archives
PHP Framework

PHP Tutorial

PHP Introduction
PHP Variables
PHP Comments
PHP Operators

PHP Conditional Statements

PHP Loops
PHP functions
PHP Array

Articles

Cakephp Overview
CodeIgniter Overview
Yii Overview
Zend Overview
PHP Security
  1. Conditional Statements

    If else statement

    The if .. else construct is used to take decision based on the Boolean result that return by a logical expression.

     

    We can use the if ...else block as following examples.

     

    Example 1

    if ($val==1)

     {

            echo “The result is 1”;

     }

    Example 2

    if ($val==’Ram’)

     {

            echo “Your name is Ram”;

     }

    else

    {

        echo “Your are not Ram”;

    }

    Example 3

    if ($val==’Ram’)

     {

            echo “Your name is Ram”;

     }

    else if ($val==’Jo’)

    {

        echo “Your name is jo”;

    }

    else

    {

        echo “Name not found”;

    }

    switch case statement

    switch case is the another conditional statement in php.It is used when we need to compare multiple values of a variable. You can use the switch case statement as following.

    <?php

        $name="Sijo";

        switch ($name)

        {

        case "Sijo":

          echo "Your name is Sijo";

          break;

        case "Dinson":

          echo "Your name is Dinson";

     

          break;

        case "Manu":

          echo "Your name is Manu";

          break;

        default:

          echo "Name not faound";

        }

        ?>

    Whenever the switch statement is executed,the variable given in the switch statement is evaluated and compared with each case constant. If one of the case constant is equal to the value of the variable given in the switch statement, control is passed to the statement following the matched case label. A break  statement is used to exit the switch statement.If none of the cases match,the default case is invoked.