Friday 10, February 2012
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    Forum    |    About Us    |    Contact Us

If Else Statement

One of the most common PHP language constructs that you will encounter is the if…then statement. The if…then statement allows you to evaluate an expression and then, depending if it is true or not, take a course of action. For example:

$x = 1;

if($x) {

      echo "True!";

}

Since $x = 1, then PHP interprets it in the context of the if statement to be a boolean type. Since 1 = true, the if statements prints out the message. Literally you can read it as "If True, then print out "True!".

Another example, but this time with an "else" clause:

$a = 5;

$b = "10";

if($a > $b) {

    echo "$a is greater than $b";

} else {

    echo "$a is not greater than $b";

}

PHP doesn't care that $a is an integer and $b is a string. It recognizes that $b also makes a pretty good integer as well. It then evaluates the expression, "If $a is greater than $b then print out that $a is greater than $b; if not (else) then print out $a is not greater than $b." It's also important to note that you don't say, "$a is less than $b," since it is possible that $a could be equal to $b. If that is the case, then the second part of the expression, the else statement, is the part that runs.

Note the use of brackets to enclose the actions. Also note the lack of semicolons where the brackets are used.

One final example is the if…elseif…else statement:

if($a == $b) {

    // do something

} elseif ($a > $b) {

    // do something else

} elseif($a < $b) {

 
     
   
PHP Tutorial
 
  Introduction
Introduction
  Variables
Variables
  Strings
Strings
  Operators
Operators
  If Else Statement
If Else Statement
  Loops
Loops
  Functions
Functions
  Forms
Forms
  Sessions
Sessions
  Cookies
Cookies
  Files
Files
  E-mail
E-mail
  Error Handling
Error Handling
  File Upload
File Upload
  PHP Databases
PHP Databases
  Connecting to a MySQL Database
Connecting to a MySQL Database
  MySQL Data Types
MySQL Data Types
  Inserting Data Into a Table
Inserting Data Into a Table
  Select Statement
Select Statement
  Where Clause
Where Clause
  Order By
Order By
  Update Statement
Update Statement
  Delete From Statement
Delete From Statement
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
  ASP Tutorial
ASP Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download The 7 Great Lies of Network Marketing Ebook The 7 Great Lies of Network Marketing
   
Download Special Report  Ebook Special Report
   
Download Data Structures and Algorithms With Object-Oriented Design Patterns in Java  Ebook Data Structures and Algorithms With Object-Oriented Design Patterns in Java
   
Download PHP Essentials  Ebook PHP Essentials
   
Download eBay Secret Tricks  Ebook eBay Secret Tricks
   
 
Studiesinn.com © 2012 All Rights Reserved.
Website Designed & Developed by TechXprtz