The Building Blocks: Data Types, Literals, Variables, and Constants - Part 2/Page 3 | WebReference

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

Figure 4.15 Mixing data types. Output from 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


  1. Variable $n is assigned a number concatenated to a string. The number is converted to a string and the two strings are joined together as one string by using the concatenation operator, resulting in the string "5 cats".
  2. Variable $years is assigned the number 9.
  3. The concatenation operator joins all the expressions into one string to be displayed. The print() function can only take one argument.
  4. The echo statement takes a list of comma-separated arguments, which causes the values of $years and $n to be displayed just as with the concatenation operator. See Figure 4.16 for the output.

Figure 4.16 Concatenation.


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

Figure 4.17 References. Output from 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


  1. Three variables are defined and assigned colors. Notice the variable names only differ by the number appended to the name, 1, 2, and 3.
  2. Although we haven't yet discussed loops, this is the best way to illustrate the use of variable variables (or dynamic variables). The initial value in the loop, $count, is set to 1. If the value of $count is less than 3 ($count ), then control goes to line 3. After the closing curly brace is reached on line 5, control will go back to the top of the loop and the value of $count will be incremented by 1. If $count is less than 3, the process repeats, and when $count reaches 3, the loop terminates.
  3. The first time through the loop, the value of $count is appended to the string "color" resulting in color1. The value, "color1", is then assigned to the variable, $primary, so that $primary = color1;.
  4. PHP expands ${$primary}a as follows:
  • ${color1} — First evaluate $primary within the curly braces.
  • $color1 — Remove the braces and now evaluate $color1 resulting in "red".
  • The color of the font and the text will be red. Next time through the loop, the count will go up by one ($count = 2) and the $color2 will be "blue", and finally $color3 will be "yellow".
  • a. The curly braces are required. If you omit them, the variable $$primary will be evaluated as $color1 but not "red".
  • See "The for Loop" on page 235 for an example of how to make use of dynamic variables with forms.
  • Figure 4.18 Dynamic variables. Output from Example 4.16.

    [previous] [next]