Programming Basics

  Home arrow Programming Basics arrow Page 3 - PHP Decision-Making
PROGRAMMING BASICS

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

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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP
    - Calendar Construction with PHP
    - PHP`s Calendar Package
    - Getting Modified Versions and Correct Dates ...
    - Combining Date Functions in PHP
    - Using PHP for Date and Time in Programming
    - More Exception Handling with PHP
    - Exception Handling in PHP
    - Error Logging and Handling Exceptions
    - Configuration Directives for Error and Excep...
    - Error and Exception Handling
    - Python Modules for Games


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap