Extras
Type Juggling
This page looks at what happens when different data types are used with the following operators:
When the concatenation operator is used, the PHP interpreter expects to be given strings. When the mathematical operators are used, it expects to be given numbers.
If the PHP interpreter is given a value that is not the expected data type, it can try to convert the value to another data type; this is known as type juggling and it can lead to unexpected results.
This page starts by demonstrating the ways in which the PHP interpreter can change values from one data type to another.
It then looks at what happens when comparison operators and equality operators are used to compare values that are different data types. The error messages shown are created by PHP 8 (some messages are different to earlier versions of PHP).
Concatenation Operator
The concatenation operator .
(a period symbol) joins one string to another string.
Below you can see what happens when one of the values is not a string.
Concatenating a String and a Number
If you join a string and a number, the number is converted to a string.
Examples
Example | Treated as | Result / Error | Description |
---|---|---|---|
'Hi ' . 1 | 'Hi ' . '1' | string(4) "Hi 1" | Integer is treated as a string. |
'Hi ' . 1.23 | 'Hi ' . '1.23' | string(7) "Hi 1.23" | Float is treated as a string. |
Concatenating a String and a Boolean
If you join a string and a boolean value of true
, then true
is treated as a string holding the value of 1
.
If you join a string and a boolean value of false
, then false
is treated as a blank string.
Examples
Example | Treated as | Result / Error | Description |
---|---|---|---|
'Hi ' . true | 'Hi ' . '1' | string(4) "Hi 1" | Boolean true is treated as a string holding the value 1 . |
'Hi ' . false | 'Hi ' . '' | string(3) "Hi " | Boolean false is treated as a blank string. |
Mathematical Operators
Mathematical operators expect to work with numbers (integers or floating point numbers).
Below you can see what happens when one of the values is not a number.
Using a Boolean instead of a Number
A boolean value of true
gets treated like a 1
, and false
is treated as 0
.
Examples
Example | Treated as | Result / Error | Description |
---|---|---|---|
1 + true | 1 + 1 | int(2) | Boolean true is treated as the integer 1. |
1 + false | 1 + 0 | int(1) | Boolean false is treated as the integer 0. |
Using a String instead of a Number
If a string is used in place of a number, the result depends on what the string contains.
The following three terms are sometimes used to describe strings that hold numbers.
- Numeric strings only contain valid integers or floating point numbers e.g.,
'123'
,'12.345'
They are treated as if they are numbers. - Leading numeric strings start with a valid integer or floating point number e.g.,
'123four'
,'12.3four'
Any leading numbers are treated as numbers, and the rest of the string is ignored. - Non-numeric strings do not start with a valid number e.g.,
'one'
,'one23'
Since PHP 8 the PHP interpreter throws aTypeError
Exception if you use a non-numeric string in place of a number.
Examples
Example | Treated as | Result / Error | Description |
---|---|---|---|
1 + '1' | 1 + 1 | int(2)
| The string holds a valid integer. It is treated as an integer. |
1 + '1.2' | 1 + 1.2 | float(2.2)
| The string holds a float. It is treated as a float. |
1 + ' 1' | 1 + 1 | int(2)
| The string starts with whitespace then holds a valid integer. It is treated as an integer. |
1 + '1.2e+3' | 1 + 1200 | float(1201)
| The string holds a float using an e (exponent). Treated as a float. |
1 + '5star' | 1 + 5 | int(6)
Warning: A non-numeric value encountered | The string holds an integer followed by other characters. The number is treated as an integer. Later characters are ignored. |
1 + '3.5star' | 1 + 3.5 | float(4.5)
Warning: A non-numeric value encountered | The string holds a float followed by other characters. The number is treated as a float. Later characters are ignored. |
1 + 'star9' | 1 + 0 | Fatal error: Uncaught TypeError: | The string starts with anything other than an integer or a float. It is treated as the number 0. |
Comparison Operators
When comparing two values using the operators <
, >
, <=
, >=
, numbers are treated as having lower values than letters.
(Since PHP 8, if you try to use a letter without quotes it will result in a TypeError
Exception.)
Examples
Example | Result / Error | Description |
---|---|---|
'a' < 100,000 | bool(false) | a is not less than 100,000 |
'z' > 100000 | bool(true) | z is greater than 100000 |
Letters are given increasing values from left to right:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Examples
Example | Result / Error | Description |
---|---|---|
'A' < 'a' | bool(true) | A is less than a |
'Z' < 'a' | bool(true) | Z is less than a |
'Abc' < 'Abd' | bool(true) | Abc is less than Abd |
'abc' < 'Abd' | bool(false) | abc is not less than Abd |
Boolean values of true
are treated like the number 1
.
Boolean values of false
are treated as the number 0
.
Examples
Example | Result / Error | Description |
---|---|---|
true < 0 | bool(false) | true is less than 0 |
false < 0 | bool(false) | false is not less than 0 (it is the same) |
Equality Operators
There are two versions of the equality operators:
- Strict equality operators
===
and!==
do not perform type juggling before comparing if two values are equal. - Loose equality operators
==
and!=
do allow type juggling before comparing if two values are equal.
When the loose equality operator is used to check if a number is equal to a string, it uses the numbers in numeric strings and leading numeric strings (as shown in the section on mathematical operators working with numbers and strings).
When the loose equality operator is used to check if two boolean values are equal, the rules in the table below are followed.
true | false | 1 | 0 | -1 | '1' | '0' | '-1' | null | array [] | 'string' | '' | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
true | true | false | true | false | true | true | false | true | false | false | true | false |
false | false | true | false | true | false | false | true | false | true | true | false | true |
1 | true | false | true | false | false | true | false | false | false | false | false | false |
0 | false | true | false | true | false | false | true | false | true | false | false | false |
-1 | true | false | false | false | true | false | false | true | false | false | false | false |
'1' | true | false | true | false | false | true | false | false | false | false | false | false |
'0' | false | true | false | true | false | false | true | false | false | false | false | false |
'-1' | true | false | false | false | true | false | false | true | false | false | false | false |
null | false | true | false | true | false | false | false | false | true | true | false | true |
array [] | false | true | false | false | false | false | false | false | true | true | false | false |
'string' | true | false | false | false | false | false | false | false | false | false | true | false |
'' | false | true | false | false | false | false | false | false | true | false | false | true |
When the strict equality operator is used to check whether or not two values are equal, the results are simpler.
Where possible, using the strict equality operators will help prevent errors where two values that are not equal get treated as being equivalent.
true | false | 1 | 0 | -1 | '1' | '0' | '-1' | null | array() | 'string' | '' | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
true | true | false | false | false | false | false | false | false | false | false | false | false |
false | false | true | false | false | false | false | false | false | false | false | false | false |
1 | false | false | true | false | false | false | false | false | false | false | false | false |
0 | false | false | false | true | false | false | false | false | false | false | false | false |
-1 | false | false | false | false | true | false | false | false | false | false | false | false |
'1' | false | false | false | false | false | true | false | false | false | false | false | false |
'0' | false | false | false | false | false | false | true | false | false | false | false | false |
'-1' | false | false | false | false | false | false | false | true | false | false | false | false |
null | false | false | false | false | false | false | false | false | true | false | false | false |
array [] | false | false | false | false | false | false | false | false | false | true | false | false |
'string' | false | false | false | false | false | false | false | false | false | false | true | false |
'' | false | false | false | false | false | false | false | false | false | false | false | true |