Programming Basics

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


    (Page 1 of 4 )

    PHP is a programming language that has become immensely popular among developers for its power and versatility when creating web-related applications. In this three-part series, you will learn about PHP statements, expressions, and operators. 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). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    In the last chapter you started to get a feel for programming with PHP and some code basics. Now it's time to expand your comfort, knowledge, and ability with PHP. We'll start with expressions and statements.

    Expressions

    There are several building blocks of coding that you need to understand: statements, expressions, and operators. A statement is code that performs a task. Statements are made up of expressions and operators. An expression is a piece of code that evaluates to a value. A value can be a number, a string of text, or a Boolean.

    A Boolean is an expression that results in a value of either TRUE or FALSE . For example, the expression 10 > 5 (10 is greater than 5) is a Boolean expression because the result is TRUE. All expressions that contain relational operators, such as the less-than sign (<), are Boolean. Some of the Boolean operators are AND, OR, and NOT. Boolean operators will be discussed at greater length later in this chapter.

    An operator is a code element that acts on an expression in some way. For instance, a minus sign
    (-) can be used to tell the computer to decrement the value of the expression after it from the expression before it. For example:

      $account_balance=$credits-$debits;

    The most important thing to understand about expressions is how to combine them into compound expressions and statements using operators. So, we're going to look at operators used to turn expressions into more complex expressions and statements.

    The simplest form of expression is a literal or a variable. A literal evaluates to itself. Some examples of literals are numbers, strings, and constants. A variable evaluates to the value assigned to it. For instance, any of the expressions in Table4-1 are valid.

    Table 4-1. Valid expressions

    Example

    Type

    1

    A numeric value literal

    "Becker Furniture"

    A string literal

    TRUE

    A constant literal

    $user_name

    A variable with username as a string, but it doesn’t necessarily have to be a string

    1+1

    A numeric value expression that evaluates to a literal

    Although a literal or variable may be a valid expression, they don't do anything. You get expressions to do things such as math or assignment by linking them together with operators.

    An operator combines simple expressions into more complex expressions by creating relationships between simple expressions that can be evaluated. For instance, if the relation you want to establish is the cumulative joining of two numeric values together, you could write 3+4.

    Figure 4-1 shows how the parts of an expression come together.


    Figure 4-1.  Operands and operators working together as an expression to form a value

    The numbers 3 and 4 are each valid expressions. Adding 3 + 4 is also a valid expression, whose value, in this case, happens to be 7. The plus sign (+) is an operator. The numbers to either side of it are its arguments, or operands. An argument or operand is something on which an operator takes action; for example, an argument or operand could be a directive from your housemate to empty the dishwasher, and the operator empties the dishwasher. Different operators have different types and numbers of operands. Operators can also be overloaded, which means that they do different things in different contexts.

    You've probably guessed from this information that two or more expressions connected by operators are called an expression. You're right, as operators create complex expressions. The more subexpressions and operators you have, the longer and more complex the expression. But no matter what, as long as it can be resolved to a value, it's still an expression.

    When expressions and operators are assembled to produce a piece of code that actually does something, you have a statement. We discussed statements in Chapter 3.They end in semicolons (;), which is the programming equivalent of ending a complete sentence with a period.

    For instance, $Margaritaville + $Sun_Tan_Application is an expression. It results in the sum of the values of $Margaritaville + $Sun_Tan_Application, but it doesn't do anything. While it's an expression, the output doesn't make any sense, but if you add the equals sign (=), $Fun_in_the_Sun=$Margaritaville + $Sun_Tan_Application;, you get a statement because the expression does something. As Example 4-1 demonstrates, it assigns the sum of the values of $Margaritaville + $Sun_Tan_Application to $Fun_in_the_Sun.

    Example 4-1. Sum of values

    <?php
    $Margaritaville = 3; // Three margaritas $Sun_Tan_Application = 2; // Two applications of sun tan
    $Fun_in_the_Sun = $Margaritaville + $Sun_Tan_Application;
    echo $Fun_in_the_Sun;
    ?>

    Example 4-1 outputs:

      5

    There really isn't much more to understand about expressions except for how to assemble them into compound expressions and statements using operators. Next, we're going to discuss operators that are used to turn expressions into more complex expressions and statements.

    More Programming Basics Articles
    More By O'Reilly Media

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - 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
    - Python in Game Development


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