Programming Basics
  Home arrow Programming Basics arrow Page 3 - PHP Decision-Making
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

PHP Decision-Making
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-10-04

    Table of Contents:
  • PHP Decision-Making
  • Operator Concepts
  • Order of precedence
  • Associativity

  • 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


    PHP Decision-Making - Order of precedence


    (Page 3 of 4 )

    The order of precedence of an operator determines which operator processes first in an expression. For instance, the multiplication and division process before addition and subtraction. You can see a simplified table at http://www.zend.com/manual/ language.operators.php#language.operators.precedence.

    If the operators have the same precedence, they are processed in the order they appear in the expression. For example, multiplication and division process in the order in which they appear in an expression because they have the same precedence. Operators with the same precedence can occur in any order without affecting the result.

    Most expressions don't have more than one operator of the same precedence level, or the order in which they process doesn't change the result. As shown in Example 4-4, when adding and subtracting the following sequence of numbers, it doesn't matter whether you add or subtract first--the result is still 1.

    Example 4-4. Order of precedence

    2 + 4 - 5 == 1;
    4 - 5 + 2 == 1;

    4 * 5 / 2 == 10;
    5 / 2 * 4 == 10;

    2 + 4 - 5 == 1;
    4 - 5 + 2 == 1;

    When using expressions that contain operators of different precedence levels, the order can change the value of the expression. You can use parentheses, ( and ), to override the precedence levels or just to make the expression easier to read. Example 4-5 shows how to change the default precedence.

    Example 4-5. Changing the default precedence using parentheses

    echo 2 * 3 + 4 + 1;
    echo 2 * (3 + 4 + 1);

    This outputs:

      11
      16

    In the second expression, the multiplication is done last because of the parentheses overriding the default order.

    PHP has several levels of precedence, enough that it's difficult to keep track of them without checking a reference. Table 4-2 is a list of PHP operators sorted by order of precedence from highest to lowest. Operators with the same level number are all of the same precedence.

    The Association column lists operators that are right-to-left instead of left-to-right. We'll discuss associativity next.

    Table 4-2. List of PHP operators

    Operator

    Description

    Operands

    Association

    Level

    NEW

    Create new object

    Constructor call

    Right to left

    1

    .

    Property access (dot notation)

    Objects

     

    2

    [ ]

    Array index

    Array, integer, or string

     

    2

    ()

    Function call

    Function or argument

     

    2

    !

    Logical NOT

    Unary

    Right to left

    3

    ~

    Bitwise NOT

    Unary

    Right to left

    3

    ++, -

    Increment and decrement operators

    1value

    Right to left

    3

    +,

    Unary plus, negation

    Number

    Right to left

    3

    (int)

    Cast operators

    Unary

    Right to left

    3

    (double)

    Cast operators

    Unary

    Right to left

    3

    (string)

    Cast operators

    Unary

    Right to left

    3

    (array)

    Cast operators

    Unary

    Right to left

    3

    (object)

    Cast operators

    Unary

    Right to left

    3

    @

    Inhibit errors

    Unary

    Right to left

    3

    *, /, %

    Multiplication, division

    Numbers

     

    4

    +,

    Addition, subtraction

    Numbers

     

    5

    .

    Concatenation

    Strings

     

    5

    <<, >>

    Bitwise shift left, bitwise shift right

    Binary

     

    6

    <, <=, >, >=

    Comparison operators

    Numbers, strings

     

    7

    ==, !=

    Equality, inequality

    Any

     

    8

    ===, !==

    Identity, nonidentity

    Any

     

    8

    &

    Bitwise AND

    Binary

     

    9

    ^

    Bitwise NOR

    Binary

     

    10

    |

    Bitwise OR

    Binary

     

    11

    &&

    Logical AND

    Boolean

     

    12

    ||

    Logical OR

    Boolean

     

    13

    ? :

    Conditional

    Boolean

    Right to left

    14

    =

    Assignment

    1value=any

    Right to left

    15

    AND

    Logical AND

    Boolean

     

    16

    OR

    Logical OR

    Boolean

     

    17

    XOR

    Logical XOR

    Boolean

     

    18

    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