Jan 20, 2021, 01:17 AM

News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!


Conditional Statements

Started by VelMurugan, May 20, 2009, 09:47 PM

previous topic - next topic
Go Down

VelMurugan

Conditional Statements

Sometimes when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. Conditional statements are the set of commands used to perform different actions based on different conditions. In this tutorial we will look at two structures: if...else and switch statements. Both perform in saw way the same task.

  •     * IF ... Else Statement


  •     * Switch Statement


  •     * Type conversion rules to Boolean




Source : webcheat

VelMurugan

If ... Else Statement

The If Statement is a way to make decisions based upon the result of a condition. For example, you might have a script that checks if boolean value is true or false, if variable contains number or string value, if an object is empty or populated, etc. The condition can be anything you choose, and you can combine conditions together to make for actions that are more complicated.

Use the if statement to execute a statement if a logical condition is true. Use the optional else clause to execute a statement if the condition is false. The syntax for If statement looks as follows:

Quote
if (condition) {
   statements_1
} else {
   statements_2
}


Condition can be any expression that evaluates to true or false. If condition evaluates to true, statements_1 are executed; otherwise, statements_2 are executed. statement_1 and statement_2 can be any statement, including further nested if statements.

You may also compound the statements using elseif to have multiple conditions tested in sequence. You should use this construction if you want to select one of many sets of lines to execute.

Quote
if (condition_1) {
   statement_1
}
[elseif (condition_2) {
   statement_2
}]
...
[elseif (condition_n_1) {
   statement_n_1
}]
[else {
   statement_n
}]


Let's have a look at the examples. The first example decides whether a student has passed an exam with a pass mark of 57:

Code: [Select]
<?php
$result 
70;

if (
$result >= 57) {
    echo 
"Pass <br />";
}
else {
    echo 
"Fail <br />";
}
?>


Next example use the elseif variant on the if statement. This allows us to test for other conditions if the first one wasn't true. The program will test each condition in sequence until:

    * It finds one that is true. In this case it executes the code for that condition.
    * It reaches an else statement. In which case it executes the code in the else statement.
    * It reaches the end of the if ... elseif ... else structure. In this case it moves to the next statement after the conditional structure.
Code: [Select]

<?php
$result 
70;

if (
$result >= 75) { 
    echo 
"Passed: Grade A <br />";
}
elseif (
$result >= 60) {
    echo 
"Passed: Grade B <br />";
}
elseif (
$result >= 45) {
    echo 
"Passed: Grade C <br />";
}
else {
    echo 
"Failed <br />";
}
?>

VelMurugan

Switch Statement

Switch statements work the same as if statements. However the difference is that they can check for multiple values. Of course you do the same with multiple if..else statements, but this is not always the best approach.

A switch statement allows a program to evaluate an expression and attempt to match the expression's value to a case label. If a match is found, the program executes the associated statement. The syntax for the switch statement as follows:

Quote
switch (expression) {
   case label_1:
      statements_1
      [break;]
   case label_2:
      statements_2
      [break;]
   ...
   default:
     statements_n
     [break;]
}


The program first looks for a case clause with a label matching the value of expression and then transfers control to that clause, executing the associated statements. If no matching label is found, the program looks for the optional default clause, and if found, transfers control to that clause, executing the associated statements. If no default clause is found, the program continues execution at the statement following the end of switch. Use break to prevent the code from running into the next case automatically.

Let's consider an example:

Code: [Select]
<?php
$flower 
"rose";
switch (
$flower)
{
  case 
"rose" 
     echo 
$flower." costs $2.50";
     break;
  case 
"daisy" 
     echo 
$flower." costs $1.25";
     break;
  case 
"orchild" 
     echo 
$flower." costs $1.50";
     break;
  default : 
     echo 
"There is no such flower in our shop";
     break;
}
?>


However, in addition to simple equality you can also test the expression for other conditions, such as greater than and less than relationships. The expression you are testing against must be repeated in the case statement. Have a look at the example:
Code: [Select]

<?php
$myNumber 
5;

switch (
$myNumber) {
  case 
0:
    echo 
"Zero is not a valid value.";
    break;
  case 
$myNumber 0:
    echo 
"Negative numbers are not allowed.";
    break;
  default:
    echo 
"Great! Ready to make calculations.";
    break;
}
?>


If an expression successfully evaluates to the values specified in more than one case statement, only the first one encountered will be executed. Once a match is made, PHP stops looking for more matches.

VelMurugan

Type conversion rules to Boolean

   1. When converting to Boolean, the following values are considered FALSE:

          * boolean False

          * integer zero (0)

          * double zero (0.0)

          * empty string and string "0"

          * array with no elements

          * object with no elements

          * special value NULL

   2. Every other value is considered TRUE.

meshikumar


Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens Web Designing Chennai | GinGly :: Build your Personal Website | CineBuzz :: Cinema News | My Kids Diary :: Kids Memories Writing :: Book Website @ 349 Rs monthly
Copyright 2005 - 2020 :: IT Acumens :: All Rights Reserved. :: Sitemap
ITAcumens Discussion Forum with 2 lakhs post running for 15 years - Powered by IT Acumens Dedicated Server