Ajax Tutorials, PHP Tutorials, Javascript Tutorials, Visual Basic.Net Training, XHTML tutorials, php script library, PHP Codes, Programming tutorials, web development tutorials
Web Development Tutorials, Programming Tutorials, PHP Script Index
Home | Forums | Downloads | Books | Submit Content | Links

Programming Articles

  PHP articles
  MySql articles
  Visual Basic.Net articles
  Java articles
  ASP articles
  ASP.Net articles
  Visual Basic articles
  CSS articles

Programming Tutorials

  PHP tutorials
  MySql tutorials
  Visual Basic.Net tutorials
  Java tutorials
  ASP tutorials
  ASP.Net tutorials
  Visual Basic tutorials
  HTML tutorials
  CSS tutorials

Programming Source Codes

  PHP scripts
  Visual Basic.Net scripts
  JavaScript scripts
  ASP scripts
  ASP.Net scripts
  Visual Basic scripts

Our Partners

  Free Resources
  Publisherbank
  Free Link Directory
  PHP Magazine
  Webmaster Passion
  Search Engine Bank
  Free Directory Site
  Free PHP Scripts
  Links Shopping Mart
  Variety Bazar
  Urdu Office Tutorials
  Evernew Scripts
  Evernew Scripts
  Evernew Directory
  Our Achievements
  Web templates
  Buy and Sell Templates
  Text Link Traders
  Directory Metro
  Gifts Choice
  Valentines Ecards
  Template Directory

Conditional Statements

Date:2006-02-13

Conditional statements are most often used to compare two or more value either true or false. conditional statements are used with comparison and logical operators.

There are two types of conditional statements:
1) if...elase statement
2) Switch statement

If Statement

If statement is very famous statement in PHP and other programming languages. This statement is used  when a condition is true and another condition is false

Syntax

  if (condition) statement;
  
  if (condition) 
        statement
  else
        statement;
        
  if (condition) 
        statement
  elseif (condition) 
        statement
  else
    statement;

Example

<html>
<body>
<?php
$a=5;
$b=10;
if ($a<=$b)
echo "Values are not equal!"; 
else
echo "Values are equal"; 
?>
</body>

</html>

In this code you can see if $a variable equal to $b then show output "values are not equal" otherwise executes else statement ("values are false"). You can also use other comparison operators as well as you can use if .. else statement with logical operators

<html>
<body>
<?php
$day1="Sat";
$day2="Sun";
if(($day1=="Sat")||($day2=="Sub")){
echo "These are holidays"; 
}else{
echo "These are Working days"; 
}
?>
</body>

</html>



This example displays multiple if else statements
<html>
<body>
<?php
 
if($operator=="+"){
$result = $operand1 + $operand2;
else if($operator=="-"){
$result = $operand1 - $operand2;
}
else if($operator=="*"){
$result = $operand1 * $operand2;
}
else if($operator=="/"){
$result = $operand1 / $operand2;
}
else
{
print "unknown operator";
}
 
      
?>
</body>

</html>


Switch Statement
If statement can contain as many elseif clauses as you need, but including many of these clauses can often create complex code, alternative is of if else statement is available. switch is a conditional statement that can have multiple branches in a much more compact format.

switch (expression) {

              case constant:

                     statements;

              ...

              case constant:

                     statements;

              default:

                     statements;

       }


Example

switch ($operator) {

case '+': $result = $operand1 +$operand2;
break;
case '-': $result = $operand1 - $operand2;
break;
case '*': $result = $operand1 * $operand2;
break;
case '/': $result = $operand1 / $operand2;
break;
default:

print "unknown operator:";
break;
}


Note: For better understanding, practices if else statement with all comparison and logical operators, then you can better understand.

Desclaimer | Privacy Policy | Submit Content | Links | Link to Us | Sitemap |Contact Us
Copyright Programming Tutorials, Web Development Tutorials, Source codes © All Rights are Reserved.
Powered By Evernew Solutions

Phoenix Landscaping - Credit Counseling - Internet Marketing - Bad Credit Loans