006 Data Types
Data types tell the PHP interpreter how to use data. You may wonder why the computer would care what kind of data is being handled. Consider how different a number should be treated as compared to text. PHP supports ten types of data.
Data Types
Scalar Types
Boolean - Either true or false
Integer - A whole number, positive or negative
Float (Double) - A real number, with a decimal in it
String - A sequence of characters
Compound Types
Array - A collection of values or variables that are accessible via an index
Object - A set of data (variables) and code (methods or procedures) that are one unit
Callable - A method that can be passed around. See the Callable docs for more.
Iterable - An object or array whose properties can be iterated over. See the Iterable docs for more.
Special Types
Resource - A special handle to an outside resource like a file or a database connection. See the Resource docs for more.
NULL - No value at all.
You can tell the type of data by using the var_dump()
or the gettype()
methods.
<?php
// An integer
var_dump(7);
// Output:
// php shell code:1:
// int(7)
echo gettype(7); // output: integer
// ^note that with gettype(), it won't output anything
// to the screen unless you have "echo" in front.
// You can also use on a variable:
$var = 3;
var_dump($var); // type: int(3)
echo gettype($var); // type: integer
$var2 = "Thomas Edison";
var_dump($var2); // type: string(13) "Thomas Edison"
echo gettype($var2); // type: string
Casting
PHP is a loosely-typed language. This means that it does not require that a variable’s type be declared before it is used, and a variable’s type can change. So a variable’s type is defined by the value it stores. If it stores a string, the variable is a string. If it stores an object, the variable is an object. This means that a variable can sometimes be changed from one type to another.
<?php
$age = 10; // $age is an integer
$age = (string) $age; // $age is cast to a string and is now "10" (note the quotes)
Casting is as simple as putting the desired type in parenthesis before the data type:
(int)
- cast to int(bool)
- cast to bool(float)
- cast to float(string)
- cast to string(array)
- cast to array(object)
- cast to object
PHP may cast to different types depending on the context. There are six contexts in which casting may automatically occur. You can read more about it in the Type Juggling documentation. We won’t go into all the contexts now, but will cover them as we go through future lessons.
We will just consider one example to illustrate the point.
<?php
// Given you have a variable with a type of object:
$obj = new stdClass(); // $obj is an object type
// What will happen when we try to echo the object to the screen?
echo $obj;
// Then this error is displayed:
// PHP Warning: Uncaught Error: Object of class stdClass could not be converted to string in php shell code:1
// Now assume you have a variable with a type of boolean:
$bool = true;
// What will happen when we try to echo the boolean to the screen?
echo $bool;
// Then this is displayed:
// 1
PHP does not automatically convert the object to a string in order to display it with echo, and it does automatically convert the Boolean to a string when it is output with echo.
Resources
Challenges
Alternative casting methods
As it happens, there is another way to cast to integers and another way to cast to strings. Search through the official documentation or google to find what they are. Once you have found them, try them out and compare them to the method described in this lesson. Which way do you prefer?