Programming Basics

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

    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