Programming Basics
  Home arrow Programming Basics arrow Page 2 - 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 - Logical operators


    (Page 2 of 4 )

    Logical operators work with the Boolean results of relational operators to build more complex logical expressions; there are four logical operators shown in Table 4-3. These operators are also Boolean operators.

    Table 4-3. Logical operators

    Logical operator Meaning
    AND TRUE if both operands must be TRUE
    OR TRUE if at least one operand is TRUE
    XOR TRUE if only one operand is TRUE
    NOT TRUE if FALSE , FALSE if TRUE

    Because they have different precedence levels, both AND and OR have two representations. AND can be given a higher precedence level as && , while OR can be given a higher precedence level as ||.

    To test whether both operands are true, use the AND operator, also represented as the double ampersands (&&) seen in Table 4-2. Both the double ampersand as well as AND are logical operators; the only difference is that the double ampersand is evaluated before the AND operator. The operators || and OR follow the same rule. TRUE is returned only if both operands are TRUE; otherwise, FALSE is returned. See Table 4-3 for more information.

    To test whether one operand is TRUE, use the OR operator, which is also represented as double vertical bars or pipes (||). TRUE is returned only if either or both operands are TRUE.

    Using the OR operator can create tricky program logic problems. If PHP finds that the first operand is TRUE, it won't evaluate the second operand. While this saves execution time, you need to be careful that the second operator doesn't contain code that needs to be executed for your program to work properly.

    To test whether only one operand is TRUE, use XOR. XOR returns TRUE if one and only one operand is TRUE. It returns FALSE if both operands are TRUE.

    To negate a Boolean value, use the NOT operator, represented as an exclamation point (!). It returns TRUE if the operand has a value of FALSE. It returns FALSE if the operand is TRUE.

    Table 4-4 displays logical statements and their results.

    Table 4-4. Logical statements and their results

    Example logical statement

    Result

    TRUE AND TRUE

    TRUE

    FALSE AND TRUE

    FALSE

    TRUE OR FALSE

    TRUE

    FALSE OR FALSE

    FALSE

    TRUE XOR TRUE

    FALSE

    TRUE XOR FALSE

    TRUE

    !TRUE

    FALSE

    !FALSE

    TRUE

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek