PHP Data Types
Values stored in a variable or in a constant are stored as a specific type of data. PHP provides eight data types. PHP is a loosely typed language. This means that the type of data that a variable can store is not specified.
Data Types
- int: A whole number or integer; numeric values without a decimal point. For example, 2 or 5
- float: Floating-point number is a numeric value with decimal point. For example, 2.3
- string: A series of characters which are surrounded by either single or double quotes
- bool: A Boolean value that can be either true or false
- null: A value that represents no value
- array: A group of values in one variable
- object: A structure created with a class
- resource: A reference that identifies a connection
PHP determines the data type automatically. When writing PHP scripts, you don't need to specify which data type you’re storing. 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 is enclosed in quotes. You can see when printing the content of the variable with var_dump.
1. Integers
Integers are whole numbers, such as 1, 10, and 333. Integers in PHP are always signed and can therefore store both positive and negative values.
$myInt = 1234; // integer
2. Floating Numbers
Floating-point numbers, also called real numbers, are numbers that contain a decimal value, such as 3.1 or .667. These can be assigned using either decimal or exponential notation.
$myFloat = 1.234;
$myFloat = 3e2; // 3*10^2 = 300
3. Strings
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 example, 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.
Escaping the Character
If you need to tell PHP to interpret the single quote (’) as a part of string instead of as the end of the string, you can do so by using a backslash (\) in front of the single quote. The backslash tells PHP that the single quote doesn’t have any special meaning. This is called escaping the character.
Single vs Double Quotes
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.
Case 1: 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,
$month = 12;
$result1 = "$month";
$result2 = '$month';
echo $result1;
echo "<br />";
echo $result2;
The output is
12
$month
Case 2: Starting a new line
The special characters \n tell PHP to start a new line. When you use double quotes, PHP starts a new line at \n; with single quotes, \n is a literal string.
Case 3: Inserting a tab
The special characters \t tell PHP to insert a tab. When you use double quotes, PHP inserts a tab at \t, but with single quotes, \t is a literal string.
Concatenation: Joining Strings
You can join strings, a process called concatenation, by using a dot (.).
4. Boolean
A Boolean data type can only be either true or false. You can assign a Boolean value to a variable as follows:
$myBool = true;
Boolean values are used when comparing values and expressions for conditional statements. 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
- The value NULL
If a variable contains a value that is not evaluated as false, it is assigned the value true.
5. Null
The case-insensitive constant null is used to represent a variable with no value.
$myNull = null; // variable is set to null
The null value evaluates differently, depending on the context in which the variable is used. If evaluated as a bool, it becomes false; as a number, it becomes zero (0); and as a string, it becomes an empty string ("").
In PHP, it is possible to use variables that have not been assigned a value. Such undefined variables are then automatically created with the null value.
echo $myUndefined; // variable is set to null
6. Array
Unlike a normal variable, which stores a single value, an array is a data structure that can hold a collection of items. You can think of an array as a list of items. These items can be of any type, such as a string, a number, a Boolean, or even another array.
$days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
print_r($days);
There are two types of arrays in PHP - indexed arrays and associative arrays. Indexed arrays have numerical indexes starting from 0, while associative arrays have string indexes.
Type Conversions
PHP automatically converts a variable’s data type as necessary, given the context in which it is used. The type of a variable or expression may be changed by performing an explicit type cast.
An explicit cast is performed by placing the desired data type in parentheses before the variable or value that is to be evaluated. In the following example, the explicit cast forces the bool variable to be evaluated as an int.
$myBool = false;
$myInt = (int)$myBool; // 0
One use for explicit casts can be seen when the bool variable is sent as output to the page. Due to automatic type conversions, the false value becomes an empty string; therefore, it is not displayed. By first converting it to an integer, the false value shows up as 0 instead.
echo $myBool; // ""
echo (int)$myBool; // "0"
Allowed Type Casts:
- (int), (integer): Cast to int
- (bool), (boolean): Cast to bool
- (float), (double), (real): Cast to float
- (string): Cast to string
- (array): Cast to array
- (object): Cast to object
- (unset): Cast to null
The array cast converts a scalar type to an array with a single element. It performs the same function as using the array constructor.
$myInt = 10;
$myArr = (array)$myInt;
$myArr = array($myInt); // same as above
echo $myArr[0]; // "10"
If a scalar type such as int is cast to object, it becomes an instance of the built-in stdClass class. The value of the variable is stored in a property of this class, called scalar.
$myObj = (object)$myInt;
echo $myObj->scalar; // "10"
The unset cast makes the variable evaluate to null. Despite its name, it does not actually unset the variable. The cast merely exists for the sake of completeness, because null is considered a data type.