Earning sources

Recent online moneymaking guides.

Unlimited Music Library, No Fees. Raydiyo (spotify Killer)

Monday, 1 July 2013

PHP Tutorial: PHP Decision Making

The if, elseif ...else and switch statements square measure wont to take call supported the various condition.

You can use conditional statements in your code to form your choices. PHP supports following threedecision creating statements:

if...else statement - use this statement if you would like to execute a group of code once a condition is true and another if the condition isn't true
elseif statement - is employed with the if...else statement to execute a group of code if one in all many condition square measure true
switch statement - is employed if you would like to pick out one in all several blocks of code to be dead, use the Switch statement. The switch statement is employed to avoid long blocks of if..elseif..else code.
The If...Else Statement

If you would like to execute some code if a condition is true and another code if a condition is fake, use the if....else statement.

Syntax
if (condition)
  code to be executed if condition is true;
else
  code to be executed if condition is false;
Example

The following example can output "Have a pleasant weekend!" if the present day is Fri, otherwise it'll output "Have a pleasant day!":
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!"; 
else
  echo "Have a nice day!"; 
?>
</body>

</html>

If quite one line ought to be dead if a condition is true/false, the lines ought to be confined among permed braces:

<html>

<body>
<?php
$d=date("D");
if ($d=="Fri")
  {
  echo "Hello!<br />"; 
  echo "Have a nice weekend!";
  echo "See you on Monday!";
  }
?>
</body>
</html>

The ElseIf Statement

If you wish to execute some code if one in every of many conditions ar true use the elseif statement

Syntax
if (condition)
  code to be executed if condition is true;
elseif (condition)
  code to be executed if condition is true;
else
  code to be executed if condition is false;

Example

The following example can output "Have a pleasant weekend!" if the present day is Friday, and "Have a pleasant Sunday!" if the present day is Sunday. Otherwise it'll output "Have a pleasant day!":
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!"; 
elseif ($d=="Sun")
  echo "Have a nice Sunday!"; 
else
  echo "Have a nice day!"; 
?>
</body>
</html>

The Switch Statement

If you wish to pick one amongst several blocks of code to be dead, use the Switch statement.

The switch statement is employed to avoid long blocks of if..elseif..else code.

Syntax
switch (expression)
{
case label1:
  code to be executed if expression = label1;
  break;  
case label2:
  code to be executed if expression = label2;
  break;
default:
  code to be executed
  if expression is different 
  from both label1 and label2;

}

Example

The switch statement works in associate degree uncommon manner. 1st it evaluates given expression then seeks a lable to match the ensuing price. If an identical price is found then the code related to the matching label are dead or if none of the lables match then statement can can execute any given default code.
<html>
<body>
<?php
$d=date("D");
switch ($d)
{
case "Mon":
  echo "Today is Monday";
  break;
case "Tue":
  echo "Today is Tuesday";
  break;
case "Wed":
  echo "Today is Wednesday";
  break;
case "Thu":
  echo "Today is Thursday";
  break;
case "Fri":
  echo "Today is Friday";
  break;
case "Sat":
  echo "Today is Saturday";
  break;
case "Sun":
  echo "Today is Sunday";
  break;
default:
  echo "Wonder which day is this ?";
}
?>
</body>
</html>




No comments:

Post a Comment

Adbox