Programming Basics
  Home arrow Programming Basics arrow Page 3 - Operators, Conditionals, and PHP Decis...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PROGRAMMING BASICS

Operators, Conditionals, and PHP Decision-Making
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-10-11

    Table of Contents:
  • Operators, Conditionals, and PHP Decision-Making
  • Logical operators
  • Conditionals
  • The else statement

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Operators, Conditionals, and PHP Decision-Making - Conditionals


    (Page 3 of 4 )

    Conditionals, like variables, form a building block in our foundation of PHP development. They alter a script's behavior according to the criteria set in the code. There are three primary conditionals in PHP:

    • if
    • ?:: (shorthand for an if statement)
    • switch

    The switch statement is useful when you have multiple things you want to do and need to take different actions based on the contents of a variable. The switch statement is discussed in more detail later in this chapter.

    The if Statement

    The if statement offers the ability to execute a block of code if the supplied condition is TRUE; otherwise, the code block doesn't execute. The condition can be any expression, including tests for nonzero, null, equality, variables, and returned values from functions.  

    No matter what, every single conditional you create includes a conditional clause. If a condition is TRUE, the code block in curly braces ({}) is executed. If not, PHP ignores it and moves to the second condition, continuing through all clauses written until PHP hits an else. Then it automatically executes that block only if the IF condition proves to be FALSE; otherwise, it moves on. The curly braces are not required if you have only one line of code to execute in the block. An else statement is not always required.

    Figure 4-2 demonstrates how an if statement works. The else block always needs to come last and be treated as if it's the default action. This is similar to the semicolon (;). Common true conditions are:

    1. $var, if $var has a value other than the empty set (0), an empty string, or NULL
    2. isset ($var), if $var has any value other than NULL, including the empty set or an empty string
    3. TRUE or any variation thereof


    Figure 4-2.  Execution branching based on an expression

    We haven't talked yet about the second bullet point. isset() is a function that checks whether a variable is set. A set variable has a value other than NULL. Table 4-2 shows comparative and logical operators, which can be used in conjunction with parentheses () to create more complicated expressions.

    The syntax for the if statement is:

      if (conditional expression){
          block of code;
      }

    If the expression in the conditional block evaluates to TRUE, the block of code that follows it executes. In this example, if the variable $username is set to 'Admin', a welcome message is printed. Otherwise, nothing happens.

      if ($username == "Admin") {
          echo ('Welcome to the admin page.');
      }

    The curly braces aren't needed if you want to execute only one statement, but it's good practice to always use them, as it makes the code easier to read and more resilient to change.

    More Programming Basics Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Learning PHP and MySQL, Second Edition,"...
     

    Buy this book now. This article is excerpted from chapter four of the book Learning PHP and MySQL, Second Edition, written by Michele Davis and Jon Phillips (O'Reilly, 2006; ISBN: 0596101104). Check it out today at your favorite bookstore. Buy this book now.

    PROGRAMMING BASICS ARTICLES

    - PHP: Hypertext Preprocessor: What is it?
    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek