PHP Manual
Prev | Chapter 6. Types | Next

# Type juggling

PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which that variable is used. That is to say, if you assign a string value to variable `var`, `var` becomes a string. If you then assign an integer value to `var`, it becomes an integer.

An example of PHP's automatic type conversion is the addition operator '+'. If any of the operands is a double, then all operands are evaluated as doubles, and the result will be a double. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does NOT change the types of the operands themselves; the only change is in how the operands are evaluated.

      1
      2 $foo = "0";  // $foo is string (ASCII 48)
      3 $foo++;      // $foo is the string "1" (ASCII 49)
      4 $foo += 1;   // $foo is now an integer (2)
      5 $foo = $foo + 1.3;  // $foo is now a double (3.3)
      6 $foo = 5 + "10 Little Piggies"; // $foo is integer (15)
      7 $foo = 5 + "10 Small Pigs";     // $foo is integer (15)
      8

If the last two examples above seem odd, see String conversion.

If you wish to force a variable to be evaluated as a certain type, see the section on Type casting. If you wish to change the type of a variable, see settype().

If you would like to test any of the examples in this section, you can cut and paste the examples and insert the following line to see for yourself what's going on:

      1
      2 echo "\$foo==$foo; type is " . gettype( $foo ) . "<br>\n";
      3

> Note: The behaviour of an automatic conversion to array is currently undefined.

>       1
>       2 $a = 1;       // $a is an integer
>       3 $a[0] = "f";  // $a becomes an array, with $a[0] holding "f"
>       4

> While the above example may seem like it should clearly result in $a becoming an array, the first element of which is 'f', consider this:

>       1
>       2 $a = "1";     // $a is a string
>       3 $a[0] = "f";  // What about string offsets? What happens?
>       4

> Since PHP supports indexing into strings via offsets using the same syntax as array indexing, the example above leads to a problem: should $a become an array with its first element being "f", or should "f" become the first character of the string $a?

> For this reason, as of PHP 3.0.12 and PHP 4.0b3-RC4, the result of this automatic conversion is considered to be undefined. Fixes are, however, being discussed.

## Type casting

Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast.

      1
      2 $foo = 10;   // $foo is an integer
      3 $bar = (double) $foo;   // $bar is a double
      4

The casts allowed are:

- (int), (integer) - cast to integer

- (real), (double), (float) - cast to double

- (string) - cast to string

- (array) - cast to array

- (object) - cast to object

Note that tabs and spaces are allowed inside the parentheses, so the following are functionally equivalent:

      1
      2 $foo = (int) $bar;
      3 $foo = ( int ) $bar;
      4

It may not be obvious exactly what will happen when casting between certain types. For instance, the following should be noted.

When casting from a scalar or a string variable to an array, the variable will become the first element of the array:

      1
      2 $var = 'ciao';
      3 $arr = (array) $var;
      4 echo $arr[0];  // outputs 'ciao'
      5

When casting from a scalar or a string variable to an object, the variable will become an attribute of the object; the attribute name will be 'scalar':

      1
      2 $var = 'ciao';
      3 $obj = (object) $var;
      4 echo $obj->scalar;  // outputs 'ciao'
      5

Prev | Home | Next
Objects | Up | Variables