Chapter2.Gaining Control with Operators and Flow Control


Chapter 2. Gaining Control with Operators and Flow Control

Chapter 1, "Essential PHP," brought you the basics of PHP, and in this chapter, we're going to really get to work with PHP. The previous chapter was about getting started; this chapter is about making things happen. Everything we'll see in this chapter is an essential skill we'll use throughout the book.

We're going to start by working with the PHP operators that let you manipulate data in this chapter. For example, when you work with numerical data, you have a whole set of operators to work withthe + operator adds numbers ($variable + 5 adds 5 to the value in $variable), the * operator multiplies numbers ($variable * 2 multiplies the value in $variable by 2), and so on. All the operators appear in Table 2-1, and knowing how to use these operators is essential knowledge in PHP.

Table 2-1. Operator Precedence

Operators

new

[

! ~ ++ -- (int) (float) (string) (array) (object)

@

* / %

+ -.

<< >>

< <= > >=

== != === !==

&

^

|

&&

||

? :

= += -= *= /= .= %= &= |= ^= <<= >>=

print

and

xor

or

,


Besides putting the PHP operators to work, we're also going to start taking command of flow control in this chapter. Flow control lets you make decisions based on your data's values, which can include data that the user enters. For example, if the value in $temperature is greater than 80, you can echo "Too hot!" like this:

 if ($temperature > 80){     echo "Too hot!"; } 

Flow control statements such as the if statement in this case give the PHP author a great deal of power because you can use them to make decisions at run time, based on the current value of the data you want to work with. They are also one of the primary ways in which PHP-enabled pages differ from static HTML pages.

Another set of flow-control statements that we'll see is loops. Computers excel at working with large amounts of data, and a loop will let you handle all the data in a huge set by working on each data item in turn. How this works will become more apparent when we work with arrays in Chapter 3, "Handling Strings and Arrays," which are designed to hold data in a way that makes it easy for loops to handle that data. Using a loop, you can handle each data item in a large set, such as the contents of a database.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net