Programming Basics
  Home arrow Programming Basics arrow 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  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
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


    (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


       · 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

    - 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
    - An Overview of Arrays in PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT