Sunday, 3 February 2013

PHP Tutorial . Chapter 5 . PHP Control Statements (Part 1 if-else statement)

What fun is programming if you don't have control over the code ? Best coding practices include writing code that responds differently to different situations . Suppose you have a code let's say , which takes in the type of an employee in an office and accordingly outputs the salary for that position , lets say , what would you do ?

( Cookies again for C Programmers , this section is actually same as the if-else you were accustomed to before !! )

The most popular control statement in any programming language is the if-else construct . So how to use the if else construct ? Let's follow the following example and things will seem pretty easy then .

Algorithm :



Example

Lets say that we have a variable that stores the type of an employee at an office . We will check that variable and print out the corresponding salary .


$a = "General Manager";

if($a=="General Manager")
echo "1000000";
else if($a=="CEO")
echo "2000000";
else if($a=="Chairman")
echo "3000000";

?>

The above code outputs 1000000 .

Also an important aspect of php code is that it continues from the last block . Suppose you have a file containing both php and html . You have a block of php code , then you have some html and then again a block of php code , php will continue from the last line of the previous block . This will be clear from the following example .


Example


$a = 5 ;

if($a==5)
{
echo "The number is equal to 5
";
$a++;
echo "Now it's value is :- ".$a."
";
}
else if($a>5)
{
?>




echo "The number is greater than 5
";
echo "Also this statement is in the second php block
";
}
else
{

echo "The number is lesser than 5
";
echo "Also this statement is in the second php block
";
}

?>

This code won't show any error even though the else-if block is spread across the two blocks . This is quite a flexible feature !! It comes in handy if you want to place some html code within the conditions or within an incomplete block .

8 comments:

  1. NICE BLOG!!! Thanks for sharing useful information about FNT Softwre Solutions and being one of best Software Training institute in Bangalore we agree that this blog is very useful for the students are Searching for best software courses, I would really like to come back again right here for like wise good articles or blog posts. Thanks for sharing...PHP Training Bangalore .

    ReplyDelete
  2. your concept is right. i think this way too. thanks for explaining it well.

    Website Development company

    ReplyDelete
  3. Nice blog...Very useful information is providing by ur blog..here is a way to find

    weblogic training in bangalore

    ReplyDelete
  4. Nice blog...Very useful information is providing by ur blog..here is a way to find

    Php training institute in bangalore

    ReplyDelete
  5. Hi, Its really very informative and useful blog..Thanks for sharing this useful information about PHP Course.

    PHP Training

    ReplyDelete
  6. Thanks for updating new information about web designing. Practically, you have shared lot of details about website designing. Also share your new updated details about website creation. Thanks mate.
    Web Designing training in Chennai

    ReplyDelete