Basic PHP Data Types


The first data type I'll introduce you to is the integer. The integer is the fundamental numeric data type in PHP and represents whole-number signed values up to a little over 2 billion. In practice, PHP will accept integer values using three mathematical bases: decimal (base 10), octal (base 8), and hexadecimal (base 16). In most situations, PHP scripts are written using decimal notation; however, in some situations octal or hexadecimal numbers make life easier. Listing 1.4 shows how each is represented in PHP:

Listing 1.4. Storing Integers in PHP
 <?php     $my_int = 50;       /* Standard Decimal Notation */     $my_int = O62;      /* Same number, Octal Notation (starts with the letter O)*/     $my_int = 0x32;     /* Hexadecimal Notation */ ?> 

When working with fractions, PHP represents the value using the floating-point data type. Floating-point numbers are defined as any number that contains a decimal fraction and that can be expressed in decimal or scientific notation, as shown in Listing 1.5:

Listing 1.5. Storing Floating Point Numbers in PHP
 <?php     /* Standard Floating Point Notation */     $my_float = 5.1;     /* Scientific Floating Point Notation of same number */     $my_float = .051e2;     ?> 

The final basic data type that is discussed in this chapter is the string. You have already been briefly exposed to strings when you examined the first real PHP example in this chapterprinting "Hello world" to the client; however, there is much more to strings than that simple example. To start off, there are two types of strings: parsed and unparsed. Parsed strings are defined using double quotes and are parsed by PHP, whereas unparsed strings are represented by single quotes and are taken as is. What does this difference mean to you as a developer? When a string is defined using double quotes (parsed), any references to variables within that string will automatically be replaced with their respective values, whereas unparsed strings will replace nothing. Listing 1.6 shows an example of both types of strings in action:

Listing 1.6. Parsed and Unparsed Strings in PHP
 <?php     $my_int = 50;     $string_one = "The value of the variable is $my_int<BR>";     $string_two = 'The value of the variable is $my_int<BR>';     echo $string_one;     echo $string_two; ?> 

When this script is executed, the output will be as follows:

 The value of the variable is 50 The value of the variable is $my_int 

As you can see, although both strings were completely identical in content, $string_one was parsed by PHP and the reference to the $my_int variable was replaced with the value of $my_int. This is in contrast to the $string_two variable, which was not parsed by PHP, and the $my_int portion of the string remained as is.

NOTE

You might have noticed that both strings also contained an HTML <BR> tag. Because you are outputting your text to a Web browser, this tag is necessary to display the two strings on separate lines. Without this HTML tag, the strings would have been jumbled together on a single linehardly the desired result.


Along with the capability to replace variable references, parsed strings enable you to work with what are called escaped characters. This special format is used to represent characters that normally would be difficult or impossible to include within a string with a standard keyboard. For instance, consider a situation that required a double-quote character in a string that you would also like to be parsed. Because the double-quote character itself is used to define the beginning and end of the string, there is no inherent way to print the double-quote character itself. This is a prime example of when to use escaped characters. In PHP, the following escape characters are allowable, as shown in Table 1.1:

Table 1.1. Escape Characters in PHP

Escape String

Resulting Character

\n

Linefeed character

\r

Carriage return character

\t

Horizontal escape character escape character escape character escape character escape character tab character

\\

The backslash character

\$

The $ character

\'

The single-quote character

\"

The double-quote character

\###

ASCII character (octal)

\x##

ASCII character (hexadecimal)


Listing 1.7 is an example of escape characters in action; note the use of the \" sequence in the second variable assignment:

NOTE

The term ASCII stands for the American Standard Code of Information Interchange and represents a standard set of characters that any computer can understand. Although some characters in the ASCII set are no longer used (at least for their original purpose), ASCII is still a standard way to reference characters. More information on ASCII (including a complete table) can be found at http://www.asciitable.com/.


Listing 1.7. Using Escaped Characters
 <?php     /* Invalid string, won't work in PHP */     $variable = "Do you know what "escaped" characters are?";     /* The same properly formatted string*/     $variable = "Do you know what \"escaped\" characters are?";     /* Prints the 'a' character using hexadecimal */     $variable = "\x41 is the 'a' character"; ?> 



PHP 5 Unleashed
PHP 5 Unleashed
ISBN: 067232511X
EAN: 2147483647
Year: 2004
Pages: 257

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