Data Types


Data items are apparently untyped in PHP because you don't have to declare types of variables, but behind the scenes, data types are important because mixing them or going beyond their allowed bounds can cause problems.

Booleans are the simplest built-in PHP data type; booleans express a truth value, which can be either TRUE or FALSE.

Integers hold a number from in the range { ...-3, -2, -1, 0, 1, 2, 3 ...} . Integers can be given in decimal (base 10), hexadecimal (base 16), or octal (base 8) notation and can include a sign (- or +). If you use the octal notation, you must precede the number with a 0 (zero); to use hexadecimal notation, you must precede the number with 0x.

Here are a few examples:

 234 -234 0123 An octal integer 0x1A A hexadecimal integer 

Floating point numbers hold floating point values. The size of a float is platform-dependent, although a maximum of about 1.8e308 with a precision of roughly 14 decimal digits is common.

Here are some examples:

 <?php     $value1 = 1.234;     $value2 = 1.23e4;     $value3 = 1E-23; ?> 

Strings are series of characters (in PHP, a character is the same as a byte). PHP imposes no practical bound to the size of strings. Strings can be specified in three different ways:

  • single quoted

  • double quoted

  • Using "heredoc" syntax

The easiest way to specify a string is to enclose it in single quotes, which just stores text in a string:

 <?php     echo 'No worries.';     echo 'Some multi-line         text looks         like this.'; ?> 

If you enclose a string in double quotes, PHP can interpret more escape sequences for special characters:

\n

Linefeed (LF or 0x0A (10) in ASCII)

\r

Carriage return (CR or 0x0D (13) in ASCII)

\t

Horizontal tab (HT or 0x09 (9) in ASCII)

\\

Backslash

\$

Dollar sign

\"

Double-quote


The values in variables can be interpolated if you put them into double-quoted (not single-quoted) strings, which means that their values are inserted directly into the string. You can do that like this:

 $value = 1; echo "The number is $value."; 

This example prints out "The number is 1."

Another way to delimit strings is by using heredoc syntax ("<<<"). You need to provide an identifier after <<<, then the string, and then the same identifier to end (note that the closing identifier must begin in the first column). Here's an example:

 echo <<<END This example uses "here document" syntax to display all the text until the ending token is reached. END; 



    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