Programming Basics
  Home arrow Programming Basics arrow Page 2 - 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 - Operator Concepts


    (Page 2 of 4 )

    PHP has many types of operators, including:

    1. Arithmetic operators
    2. Array operators
    3. Assignment operators
    4. Bitwise operators
    5. Comparison operators
    6. Execution operators
    7. Incrementing/decrementing operators
    8. Logical operators
    9. String operators

    The operators are listed as found on http://www.php.net/manual/en/language.operators.php. There are some operators we're not going to discuss so you can get up and running with PHP as quickly as possible. These include some of the casting operators that we'll just skim the surface of for now. Each operator has four critical properties in addition to its core functionality:

    1. Number of operands
    2. Type of operands
    3. Order of precedence
    4. Operator associativity

    The easiest place to start is by talking about the operands.

    Number of Operands

    Different operands take different numbers of operands. Many operators are used to combine two expressions into a more complex single expression; these are called binary operators. Binary operators include addition, subtraction, multiplication, and division.

    Other operators take only one operand; these are called unary operators. Think of the negation operator (-) that multiplies a numeric value by -1. The preincrement and predecrement operators described in Chapter 3 are also unary operators.

    A ternary operator takes three operands. The shorthand for an if statement, which we'll talk about later when discussing conditionals, takes three operands.

    Types of Operands

    You need to be mindful of the type of operand on which an operator is meant to work because certain operators expect their operands to be of particular data types. PHP attempts to make your life as easy as possible by automatically converting operands to the data type that an operator is expecting. There are times, however, that an automatic conversion isn't possible.

    Mathematical operators are an example of where you need to be careful with your types. They take only numbers as operands. For example, when you try to multiply two strings, PHP can convert the strings to numbers. While "Becker" * "Furniture" is not a valid expression, it returns zero. On the other hand, an expression that is converted without an error is
    "70" * "80". This evaluates to 5600 . Although 70 and 80 are strings, PHP is able to convert them to the number type required by the mathematical operator.

    There will be times when you want to explicitly set or convert a variable's type. There are two ways to do this in PHP: first, by using settype to actually change the data type; or second, by casting, which temporarily converts the value. PHP uses casting to convert data types. When PHP does the casting for you automatically, it's called implicit casting. You can also specify data types explicitly, but it's not something that you'll likely need to do.

    PHP uses implicit casting to cast to the type that the operator requires.

    The cast types allowed are:

    (int), (integer)
       Cast to integer, whole numbers without a decimal part.

    (bool), (boolean) 
       Cast to Boolean.

    (float), (double), (real) 
       Cast to float, numbers that may include a decimal
       part.

    (string)
       Cast to string.

    (array)
       Cast to array.

    (object)
       Cast to object.

    To use a cast, place it before the variable to cast, as shown in Example 4-2. The $test_string variable contains the string 1234.

    Example 4-2. Casting a variable

    $test=1234;
    $test_string = (string)$test;

    Keep in mind that it may not always be obvious what will happen when casting between certain types. You might run into problems if you don't watch yourself when manipulating variable types.

    Some binary operators, such as the assignment operators, have further restrictions on the lefthand operand. Because the assignment operator is assigning a value to the lefthand operator, it must be something that can take a value, such as a variable. Example 4-3 demonstrates good and bad lefthand expressions.

    Example 4-3. Lefthand expressions

    3 = $locations; // bad - a value cannot be assigned to the literal 3
    $a + $b = $c; //bad - the expression on the left isn't one variable
    $c = $a + $b; //OK
    $stores = "Becker"." "."Furniture"; // OK

    There is a simpler way to remember this. The lefthand expression in assignment operations is known as an
    L-value. L-values in PHP are variables, elements of an array, and object properties. Don't worry about object properties.

    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