PHP Data Types
. Last modified on August 25th, 2022.
This is one boring topic we have in all programming languages – a list of data types. It is so boring that no one cares to read it once completed. Sometimes surprisingly developers discover that they have not used a particular data type.
Data types are generally used to represent the type of data associated with PHP variables, function’s return type/parameters and etc. We get accustomed to them over the period of use.
To the better of all PHP is a loosely typed language, there is no need of specifying data type while declaring variables.
PHP Primitive Data Types
There are eight primitive data types in PHP and it is classified into 3 types.
- Scalar Data Types
- Compound Data Types
- Special Data Types
1. Scalar Data Types
Scalar data types will contain only a single value for variable references.
- Integer – PHP integer data type will be used for representing non-fractional numeric values. The range of integers varies with respect to the platform. That is, 2^32 – 2^31 for 32-bit, and, 2^64 – 2^63 for 64-bit.
- Float/Double – These data types will represent fractions. For example, 8.99, 0.1 and etc. The double data type supports fractions with more decimal digits.
- String – A sequence of bytes enclosed by a pair of single or double quotes is called a PHP string. We can also use heredoc or nowdoc. But double quotes support variable interpolation.
- Boolean – It contains case insensitive TRUE or FALSE values. Zero represents FALSE and any non-zero value represents boolean TRUE.
2. Compound Data Types
Compound data type, as per its name, will have a group of data in the same type.
- Array – PHP Arrays are used to contain a group of values with the same data type. This group of values can be specified with default numeric indices or some associative indices.
- Object – This is another compound data type in PHP. Using PHP object data type, we can have a collection of properties set with it.
3. Special Data Types
- Resource – PHP resource datatype is used to refer to external resource data. For example, file resources, database resources and etc.
- NULL – This type of data contains PHP constant NULL which is case insensitive. We can use the is_null() function to check any value if it is NULL.
Pseudo-Types in PHP
Apart from the above major classifications, PHP includes pseudo data types. For that, PHP provides keywords that we can see with the syntax of PHP functions. These are used for representing the function’s arguments type or return type.
- void – We know very well that it is representing empty parameters or return type.
- mixed – It indicates a PHP function supporting more data types for its arguments and data type.
- number – This keyword indicates that a function will accept any type of numeric parameter either integer or float and the same for its return type.
- callback – This keyword represents that the parameter is having the name of the callback function. But, we should use user-defined function names, and not pre-defined PHP constructs like echo(), print()…
Miscellaneous: Data Type Related Functions in PHP
- gettype() – data type getter returns given variables data types.
- var_dump() – This function used to get the data type of an expression.
- is_<type>() – There are several variable functions like is_array(), is_null() and etc., to check a variable with respect to specific data type.
- settype() – This is the data type setter, used in PHP type conversion.
↑ Back to Top