The Building Blocks: Data Types, Literals, Variables, and Constants - Part 2/Page 3
[previous] [next]
The Building Blocks:
Data Types, Literals, Variables, and Constants - Part 2
Variables and Mixed Data Types
Remember, strongly typed languages like C++ and Java require that you specify the type of data you are going to store in a variable when you declare it, but PHP is loosely typed. It doesn't expect or allow you to specify the data type when declaring a variable. You can assign a string to a variable and later assign a numeric value. PHP doesn't care and at runtime, the PHP interpreter will convert the data to the correct type. In Example 4.12, consider the following variable, initialized to the floating-point value of 5.5. In each successive statement, PHP will convert the type to the proper data type (see Table 4.3).
Table 4.3 How PHP Converts Data Types
Variable Assignment | Conversion |
$item = 5.5; | Assigned a float |
$item = 44; | Converted to integer |
$item = "Today was bummer"; | Converted to string |
$item = true; | Converted to boolean |
$item = NULL; | Converted to the null value |
Example 4.12 demonstrates data type conversion. The gettype built-in function is used to display the data types after PHP has converted the data to the correct type. See Figure 4.15 for the output.
Example 4.12
Type Casting. Like C and Java, PHP provides a method to force the conversion of one type of value to another using the cast operator (see Chapter 5, "Operators").
Concatenation and Variables
To concatenate variables and strings together on the same line, the dot (.) is used. The dot is an operator because it operates on the expression on either side of it (each called an operand). In expressions involving numeric and string values with the dot operator, PHP converts numeric values to strings. For example, consider the following statements:
Example 4.13
Explanation
|
References
Another way to assign a value to a variable is to create a reference (PHP 4). A reference is when one variable is an alias or pointer to another variable; that is, they point to the same underlying data. Changing one variable automatically changes the other. This might be useful in speeding things up when using large arrays and objects, but for now, we will not need to use references.
To assign by reference, prepend an ampersand (&
) to the beginning of the old variable that will be assigned to the new variable; for example, $ref = & $old;
. See Example 4.14 and it's output in Figure 4.17.
Example 4.14
One important thing to note is that only named variables can be assigned by reference, as shown in Example 4.15.
Example 4.15
Variable Variables (Dynamic Variables)
A variable variable is also called a dynamic variable. It is a variable whose name is stored in another variable. By using two dollar signs, the variable variable can access the value of the original variable. Consider the following example:
Dynamic variables are useful when you are dealing with variables that all contain a similar name such as form variables. Curly braces are used to ensure that the PHP parser will evaluate the dollar signs properly; that is, $clown
will be evaluated first, and the first dollar sign removed, resulting in ${pet}
, and finally $pet
will be evaluated to "Bozo"
. Example 4.16 demonstrates how variable variables can be used dynamically to change the color of a font. Output is shown in Figure 4.18.
Example 4.16
Explanation
|
[previous] [next]
URL: