Handling Data Types


You don't decide the way your data is stored internallyPHP does. Behind the scenes, it supports eight internal data types (we'll see all these types in this book):

  • boolean. Holds true/false values

  • integer. Holds numbers such as -1, 0, 5 and so on

  • float. Holds floating-point numbers ("doubles") such as 3.14159 or 2.7128

  • string. Holds text such as "Hello from PHP."

  • array. Holds arrays of data items

  • object. Holds programming objects

  • resource. Holds a data resource

  • NULL. Holds a value of NULL

You usually don't have to worry about these data types because PHP determines a variable's type based on the kind of data you assign to it. For example, this makes $variable a string:

 $variable = "No worries."; 

This makes it a float value:

 $variable = 1.2345; 

This makes it a boolean value:

 $variable = TRUE; 

Because PHP selects the data type based on the data you assign to a variable, there's no problem here. The trouble starts when you mix data types by, for example, adding new values to the value in $variable using the + (addition) operator, which we'll see in Chapter 3. Here are some examples:

 <?php $variable = "0";              // $variable is a string set to "0" $variable = $variable + 2;    // $variable is now an integer set to 2 $variable = $variable + 1.1;  // $variable is now a float set to 3.1 $variable = 2 + "8 apples";   // $variable is now an integer set to 10 ?> 

If you want to avoid potential data type troubles, don't mix data types. Even if you do, PHP does the right thing almost every time (such as converting the result of adding an integer and a float into a float value, which is the right thing to do), but if you need to explicitly specify the type of a variable to PHP, you can always use a type cast. Casts appear inside parentheses and come right before the name of the variable whose type you want to specify. Here are a few examples:

 $int_variable = (integer) $variable; $float_variable = (float) $variable; $string_variable = (string) $variable; 

Some hints about mixing data types: when you're converting to the boolean type, these values are considered FALSE (more on these items, like arrays, is coming up in the book):

  • The boolean FALSE

  • The integer 0

  • The float 0.0

  • The empty string, and the string "0"

  • An array with zero elements

  • An object with no member variables

  • The special type NULL (including unset variables)

Every other value is considered trUE (including any resource). When you're converting to the integer type:

  • Boolean FALSE will yield 0 (zero), and boolean TRUE will yield 1 (one).

  • Float values will be rounded toward zero.

When you're converting to the float type, the conversion is the same as if the value was converted to an integer and then to a float. You can also convert from string to the numeric types, but that's somewhat involvedsee Chapter 3 for all the details.

In the next chapter, we're going to start getting control over the data in our PHP scripts by using operators and the PHP statements that control program flow and loopingall vital items for any PHP programmer.



    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