PHP Tutorial Part 5 [if, if-else, elseif and switch statements]

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 5.00 out of 5)
Loading ... Loading ...

PHP Video Tutorial Part 5

Part 5: Documentation + Video  [if, if-else, elseif and switch statements]

YouTube Preview Image

Download Link :

Part 5  download

Size: 5.65 MB
And Also  you can check all older and Incoming tutorials here :

Tutorial Links

 

DOCUMENTATION

Hello Friends,

Welcome To PHP Tutorial Part 5

Topic Covered: if, if-else, elseif and switch statements

Well I think you read my first tutorial PHP part 1, 2, 3 if not then kindly download from here:

Download Older One’s

  • Go to PHP folder and download rest all Parts of PHP tutorial.

____________________________________________________________________________________________

Today we will discuss if, if-else, elseif and switch statements

1. If Statement:

If you want to execute some code if the condition is true, then use the if statement.  Let’s take a look at syntax:

if and if else syntax

The code will execute only if the condition is true, In this case the condition will not satisfied so the code will not execute.

<?php

$a=2;

$b=3;

if ($a > $b) {

echo “a is greater than b”;

}

?>

You can check in this case there is no output. So just swap the values. And you will find it will show you “a is greater than b”.You can Try Using the other Boolean expressions also i.e. Logical and comparison operators :)

1. If – else statements :

If you want to execute some code if one condition is true, and in another code if first condition is false, use the if – else statement.

Take a look above at syntax:

<?php

$a=3;

$b=2;

if ($a > $b){

echo “a is greater than b”;

}

else {

echo “b is greater than a”;

}

?>

3. ElseIf Statement:

We have another option if you want to execute some code in which more than one or several conditions are true then use the elseif statement.lets check the syntax of elseif:

if else if syntax

4. Switch Statement:

If you want to execute one statement from number of statements, then use the Switch statement. This is more efficient method than previous one. Take a look at syntax:

switch syntax

If the expression has value equal to value 1 then it will execute the statement inside it and break is used to terminate and go. If the expression has not equal to any value then it will execute default loop. In default its optional break.

Take a look at program:

<?php

$a = 2;

switch ($a)

{

case 0:

echo “a has value = 0″;

break;

case 1:

echo “a has value = 1″;

break;

case 2:

echo “a has value = 2″;

break;

default:

echo “a is not equal to 0,1,2″;

break;      // optional using break at the last

}

?>

Hope you enjoying the tutorial.

If any problems mail me at:       encarta.mann@gmail.com

Thanks for reading this documentation.                                                           Continue in Next Edition ….


Thanks,

LM Team!

If you like it, then why not share with others and bookmark it :

  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogosphere News
  • email
  • Internetmedia
  • LinkedIn
  • Live
  • MSN Reporter
  • MySpace
  • PDF
  • SphereIt
  • StumbleUpon
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • RSS
  • blogmarks
  • Hi. I like the way you write. Will you post some more articles?

  • interesting material, where such topics do you find? I will often go

  • Its not find! i himself made all :P

  • good,
    thank you for the information,
    This was helped,
    spirit! ! ! ! !

  • thank you for the information,
    This was helped,
    spirit! ! ! ! !

You can follow any responses to this entry through the RSS 2.0 feed.