Values stored in a variable or a constant are stored as a specific type of data. PHP provides eight data types:
Integer: A whole number
Floating-point number: A numeric value with decimal digits
String: A series of characters
Boolean: A value that can be either true or false
Array: A group of values in one variable
Object: A structure created with a class
NULL: A value that represents no value
Integer, float, string, Boolean, and NULL data types are discussed in the following sections. Arrays are discussed in the section "Using Arrays," later in this chapter. Objects are discussed in Chapter 4 in this minibook.
When writing PHP scripts, you don't need to specify which data type you're storing. PHP determines the data type automatically. The following two statements store different data types:
$var1 = 123; $var2 = "123";
The value for $var1 is stored as an integer. The value for $var2 is stored as a string because it's enclosed in quotes.
PHP converts data types automatically when it needs to. For instance, if you add two variables, one containing an integer and one containing a float, PHP converts the integer to a float so that it can add the two.
Occasionally, you might want to store a value as a data type different than the data type PHP automatically stores. You can set the data type for a variable with a cast, as follows:
$var3 = "222"; $var4 = (int) $var3;
This statement sets $var4 equal to the value in $var3, changing the value from a string to an integer. You can also cast using (float) or (string).
You can find out which data type is stored in a variable with var_dump(). For instance, you can display a variable as follows:
var_dump($var4);
The output from this statement is the following:
int(222)
Integers are whole numbers, such as 1, 10, and 333. Floating-point numbers, also called real numbers, are numbers that contain a decimal value, such as 3.1 or .667. PHP stores the value as an integer or a float automatically.
PHP allows you to do arithmetic operations on numbers. You indicate arithmetic operations with two numbers and an arithmetic operator. For instance, one operator is the plus (+) sign, so you can indicate an arithmetic operation like this:
1 + 2
You can also perform arithmetic operations with variables that contain numbers, as follows:
$n1 = 1; $n2 = 2; $sum = $n1 + $n2;
You can add numbers that aren't the same data type, as follows:
$n1 = 1.5; $n2 = 2; $sum = $n1 + $n2;
PHP converts $n2 to a float (2.0) and adds the two values. $sum is then a float.
PHP provides five arithmetic operators. Table 1-4 shows the arithmetic operators that you can use.
You can do several arithmetic operations at once. For instance, the following statement performs three operations:
$result = 1 + 2 * 4 + 1;
The order in which the arithmetic is performed is important. You can get different results depending on which operation is performed first. PHP does multiplication and division first, followed by addition and subtraction. If other considerations are equal, PHP goes from left to right. Consequently, the preceding statement sets $result to 10, in the following order:
$result = 1 + 2 * 4 + 1 (first it does the multiplication) $result = 1 + 8 + 1 (next it does the leftmost addition) $result = 9 + 1 (next it does the remaining addition) $result = 10
You can change the order in which the arithmetic is performed by using parentheses. The arithmetic inside the parentheses is performed first. For instance, you can write the previous statement with parentheses like this:
$result = (1 + 2) * 4 + 1;
This statement sets $result to 13, in the following order:
$result = (1 + 2) * 4 + 1 (first it does the math in the parentheses) $result = 3 * 4 + 1 (next it does the multiplication) $result = 12 + 1 (next it does the addition) $result = 13
|
Often, the numbers that you work with are dollar amounts, such as product prices. You want your customers to see prices in the proper format on Web pages. In other words, dollar amounts should always have two decimal places. However, PHP stores and displays numbers in the most efficient format. If the number is 10.00, it's displayed as 10. To put numbers into the proper format for dollars, you can use sprintf. The following statement formats a number into a dollar format:
$newvariablename = sprintf("%01.2f", $oldvariablename);
This statement reformats the number in $oldvariablename and stores it in the new format in $newvariablename, which is a string data type. For example, the following statements display money in the correct format:
$price = 25; $f_price = sprintf("%01.2f",$price); echo "$f_price";
You see the following on the Web page:
25.00
If you display the variable with var_dump($f_price), the output is
string(5) "25.00"
If you want commas to separate thousands in your number, you can use number_format. The following statement creates a dollar format with commas:
$price = 25000; $f_price = number_format($price,2); echo "$f_price";
You see the following on the Web page:
25,000.00
The 2 in the number_format statement sets the format to two decimal places. You can use any number to get any number of decimal places.
A character string is a series of characters. Characters are letters, numbers, and punctuation. When a number is used as a character, it is just a stored character, the same as a letter. It can't be used in arithmetic. For instance, a phone number is stored as a character string because it needs to be only stored — not added or multiplied.
When you store a character string in a variable, you tell PHP where the string begins and ends by using double quotes or single quotes. For instance, the following two statements produce the same result:
$string = "Hello World!"; $string = 'Hello World!';
Suppose that you wanted to store a string as follows:
$string = 'It is Sally's house'; echo $string;
These statements won't work because when PHP sees the ' (single quote) after Sally, it thinks that this is the end of the string, displaying the following:
It is Sally
You need to tell PHP to interpret the single quote (') as an apostrophe instead of as the end of the string. You can do this by using a backslash () in front of the single quote. The backslash tells PHP that the single quote doesn't have any special meaning; it's just an apostrophe. This is called escaping the character. Use the following statements to display the entire string:
$string = 'It is Sally's house'; echo $string;
NOTE
Similarly, when you enclose a string in double quotes, you must also use a backslash in front of any double quotes in the string.
Single-quoted and double-quoted strings are handled differently. Single-quoted strings are stored literally, with the exception of ', which is stored as an apostrophe. In double-quoted strings, variables and some special characters are evaluated before the string is stored. Here are the most important differences in the use of double or single quotes in code:
Handling variables: If you enclose a variable in double quotes, PHP uses the value of the variable. However, if you enclose a variable in single quotes, PHP uses the literal variable name. For example, if you use the following statements:
$month = 12; $result1 = "$month"; $result2 = '$month'; echo $result1; echo "<br />"; echo $result2;
the output is
12 $month
Refer to Table 1-3, earlier in this chapter, for more examples.
Starting a new line: The special characters tell PHP to start a new line. When you use double quotes, PHP starts a new line at ; with single quotes, is a literal string. For instance, when using the following statements:
$string1 = "String in double quotes"; $string2 = 'String in single quotes';
the string1 output is
String in double quotes
and the string2 output is
String in single quotes
Inserting a tab: The special characters tell PHP to insert a tab. When you use double quotes, PHP inserts a tab at , but with single quotes, is a literal string. For instance, when using the following statements:
$string1 = "String in double quotes"; $string2 = 'String in single quotes';
String in double quotes
and the string2 output is
String in single quotes
The quotes that enclose the entire string determine the treatment of variables and special characters, even if other sets of quotes are inside the string. For example, look at the following statements:
$number = 10; $string1 = "There are '$number' people in line."; $string2 = 'There are "$number" people waiting.'; echo $string1,"<br /> "; echo $string2;
The output is as follows:
There are '10' people in line. There are "$number" people waiting.
You can join strings, a process called concatenation, by using a dot (.). For instance, you can join strings with the following statements:
$string1 = 'Hello'; $string2 = 'World!'; $stringall = $string1.$string2; echo $stringall;
The echo statement's output is
HelloWorld!
Notice that no space appears between Hello and World. That's because no spaces are included in the two strings that are joined. You can add a space between the words by using the following concatenation statement rather than the earlier statement:
$stringall = $string1." ".$string2;
You can use .= to add characters to an existing string. For example, you can use the following statements in place of the preceding statements:
$stringall = "Hello"; $stringall .= " World!"; echo $stringall;
The echo statement output is this:
Hello World!
You can also take strings apart. You can separate them at a given character or look for a substring in a string. You use functions to perform these and other operations on a string. We explain functions in Chapter 2 in this minibook.
PHP provides a feature called a heredoc that is useful for assigning values that consist of really long strings that span several lines. A heredoc enables you to tell PHP where to start and end reading a string. A heredoc statement has the following format:
$varname = <<<ENDSTRING text ENDSTRING;
ENDSTRING is any string you want to use. You enclose the text you want stored in the variable $varname by typing ENDSTRING at the beginning and again at the end. When PHP processes the heredoc, it reads the first ENDSTRING and knows to start reading text into $varname. It continues reading text into $varname until it encounters the same ENDSTRING again. At that point, it ends the string. The string created by a heredoc statement evaluates variables and special characters in the same manner as a double-quoted string.
The following statements create a string with the heredoc method:
$distance = 10; $herevariable = <<<ENDOFTEXT The distance between Los Angeles and Pasadena Is $distance miles. ENDOFTEXT; Echo $herevariable;
The output of the echo statement is as follows:
The distance between Los Angeles and Pasadena is 10 miles.
But be careful. PHP is picky about its ENDSTRINGs. When it first appears, the ENDSTRING (ENDOFTEXT in this example) must occur at the end of the first line, with nothing following it, not even a space. And the ENDSTRING on the last line must occur at the start of the line, with nothing before it, not even a space and nothing following it other than the semicolon. If these rules are broken, PHP won't recognize the ending string and will continue looking for it throughout the rest of the script. It will eventually display a parse error showing a line number that is the last line in the script.
A Boolean data type takes on only the values of true or false. You can assign a Boolean value to a variable as follows:
$var1 = true;
PHP sets the variable to a Boolean data type. Boolean values are used when comparing values and expressions for conditional statements, such as if statements. Comparing values is discussed in detail in Chapter 2 in this minibook.
The following values are evaluated as false by PHP:
The word false
The integer 0
The floating-point number 0.0
An empty string
A string with the value 0
An empty array
An empty object
If a variable contains a value that is not evaluated as false, it is assigned the value true.
The only value that is a NULL data type is NULL. You can assign the value to a variable as follows:
$var1 = NULL;
A variable with a NULL value contains no value.
3.141.2.157