Programming Basics

  Home arrow Programming Basics arrow Page 4 - 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 - Associativity


    (Page 4 of 4 )

     

    All operators process their operators in a certain direction. This direction is called associativity, and it depends on the type of operator. Most operators are processed from left to right, which is called left associativity. For example, in the expression 3 + 5
    - 2
    , 3 and 5 are added together, and then 2 is subtracted from the result, evaluating to 8. While left associativity means that the expression is evaluated from left to right, right associativity means the opposite.

    Since the assignment operator has right associativity, it is one of the exceptions because right associativity is less common. The expression $a=$b=$c processes by $b being assigned the value of $c, and then $a being assigned the value of $b. This assigns the same value to all of the variables. If the assignment operator is right associative, the variables might not have the same value.

    If you're thinking that this is incredibly complicated, don't worry. These rules are enforced only if you fail to be explicit about your instructions. Keep in mind that you should always use brackets in your expressions to make your actual meaning clearer. This helps both PHP and also other people who may need to read your code.

    If you accidentally use & instead of &&, or | instead of ||, you'll end up getting the wrong operator. & and | compare binary data bit by bit. PHP will convert your operands into binary data and apply binary operators.

    Relational Operators

    In Chapter 3 we discussed assignment and math operators. Relational operators provide the ability to compare two operands and return either TRUE or FALSE regarding the comparison. An expression that returns only TRUE or FALSE is called a Boolean expression, which we discussed in the previous chapter. These comparisons include tests for equality and less than or greater than. These comparison operators allow you to tell PHP when to do something based on whether a comparison is true so that decisions can be made in your code.

    Please check back next week for the continuation of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    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 2 - Follow our Sitemap