Saturday, November 1, 2008

Variables and Data Types

Data types form the backbone of any programming language, providing the programmer with a means by which to represent various types of information. PHP provides support for six general data types:
• Integers
• Floating-point numbers
• Strings
• Arrays
• Objects
• Booleans

One of the pillars of any programming language is its support for numbers. PHP supports both integers and real (double) numbers. Each of these number formats is described in further detail later.

Integer Values


An integer is nothing more than a whole number. Integers are represented as a series of one or more digits. Some examples of integers are:
5
591
52
31

Octals and Hexadecimals


Integers in octal (base 8) and hexadecimal (base 16) formats are supported. Octal values begin with the digit 0, followed by a sequence of digits, each ranging from 0 to 7. Some examples of octal integers are:
0422
0534
Hexadecimal integers can consist of the digits 0 through 9 or the letters a (A) through f (F). All hexadecimal integers are preceded by 0x or 0X. Some examples of hexadecimal integers are:
0x3FF
0X22abc

Floating-Point Numbers


A floating-point number is essentially a real numbers, that is, a number denoted either wholly or in part by a fraction. Floating-point numbers are useful for representing values that call for a more accurate representation, such as temperature or monetary figures. PHP supports two floating-point formats: standard notation and scientific notation.

Standard Notation


Standard notation is a convenient representation typically used for real numbers, for example, monetary values. Some examples are:
12.45
98.6

Scientific Notation


Scientific notation is a more convenient representation for very large and very small numbers, such as interplanetary distances or atomic measurements. Some examples include:
3e8
5.9736e24
Chapter 2
32

String Values


A string is a group of characters that are represented as a single entity but can also be examined on a character-by-character basis. Some examples of strings are: thesaurus
49ers
abc
&%/$£
Note that PHP doesn’t include support for the char data type. Rather, the string data type can be considered the all-encompassing type that represents both single and multiple character sets.

String Assignments


Strings can be delimited in two ways, using either double quotation marks (“”) or single quotation marks (‘’). There are two fundamental differences between the two methods. First, variables in a double-quoted string will be replaced with their respective values, whereas the single-quoted strings will be interpreted exactly as is, even if variables are enclosed in the string. The following two string declarations produce the same result:
$food = "meatloaf";
$food = 'meatloaf';
However, the following two declarations result in two drastically different outcomes:
$sentence = "My favorite food is $food";
$sentence2 = 'My favorite food is $food';
The following string is what exactly will be assigned to $sentence. Notice how the variable $food is automatically interpreted:
My favorite food is meatloaf.
Whereas $sentence2 will be assigned the string as follows:
My favorite food is $food.
In contrast with $sentence, the uninterpreted variable $food will appear in the string assigned to $sentence2. These differing outcomes are due to the usage of double and single quotation marks in assigning the corresponding strings to $sentence and $sentence2. Before discussing the second fundamental difference between doublequoted and single-quoted strings, an introduction of PHP’s supported string delimiters is in order. As with most other mainstream languages, a set of delimiters is used to represent special characters, such as the tab or newline characters.

No comments: