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 .

23 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
  7. Thanks for sharing these niche piece of coding to our knowledge. Here, I had a solution for my inconclusive problems & it’s really helps me a lot keep updates…
    Web design courses in Chennai

    ReplyDelete
  8. the discussion about the control statement everything was crystal clear keep sharing.
    Web design training in Chennai

    ReplyDelete
  9. HTML5 Courses in Chennai Training

    Hi, Thanks for sharing this valuable blog.I was really impressed by reading this blog. I did HTML5 Training in Chennai at reputed HTML5 Training Institutes in Chennai. This is really useful for me to make a bright future in designing field.

    HTML Training in Chennai

    ReplyDelete
  10. PHP Training Chennai

    I get a lot of great information from this blog. Thank you for your sharing this informative blog. Recently I did PHP course at a leading academy. If you are looking for best PHP Training Institute in Chennai visit FITA IT training academy which offer real timePHP Training in Chennai.

    PHP Course in Chennai

    ReplyDelete
  11. Are you looking for Best DOT NET Training in Chennai? Let us know we are ready to serve for your career. FITA is pleased to inform you that; we provides practical training on all the technologies with the MNC exports having more than 5 years of experience in your preferred domain. Get your career with our knowledge.
    DOT NET Training Institutes in Chennai|DOT NET Training Chennai

    ReplyDelete
  12. This is a wonderful informative blog here i had chance to get useful information keep posting...
    JAVA Training in Chennai|JAVA Training Institutes in Chennai

    ReplyDelete
  13. Android Training in Velachery

    Your blog is really useful for me. Thanks for sharing this useful blog..Suppose if anyone interested to learn Android Course in Chennai please visit fita academy which offers best Android Training in Chennai at reasonable cost.

    Android Training Institutes in Chennai

    ReplyDelete
  14. Dot Net Training Chennai

    Thanks for your wonderful post.It is really very helpful for us and I have gathered some important information from this blog.If anyone wants to get Dot Net Training in Chennai reach FITA, rated as No.1 Dot Net Training Institute in Chennai.

    Dot Net Course in Chennai


    ReplyDelete
  15. This post having niche information to make myself updated. Angularjs is the extended version of HTML with new attributes. It’s a JavaScript framework that can be added to an html page with a tag of script.if you want to study
    Angular training in chennai get into FITA.
    Angular course in chennai

    ReplyDelete
  16. Digital Marketing Training in Chennai

    Thanks for sharing this informative blog. Recently I did Digital Marketing courses in Chennai at a leading digital marketing company. It's really useful for me to make a bright career. If anyone wants to get Digital Marketing Training in Chennai visit infiniX.

    Regards...

    Digital Marketing Course in Chennai

    ReplyDelete
  17. Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for Learners..
    Mysql Training in chennai | Mysql Training chennai | Mysql course in chennai | Mysql course chennai

    ReplyDelete
  18. Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post.very nice blogs!!!PHP Training in chennai | PHP Training chennai | PHP course in chennai | PHP course chennai


    ReplyDelete
  19. Thanks for sharing informative article on wordpress. Being a most advanced CMS tool, wordpress help you to create sophisticated website with ease. Wordpress Training in Chennai

    ReplyDelete