PHP Data Types
Last modified on February 24th, 2017 by Vincy.
This is one boring topic we have in all programming languages – list of data types. It is so boring that no one cares to read it once completely. 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 an only single value for variable references.
- Integer – PHP integer data type will be used for representing non-fractional numeric values. The range of integer 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 type will represent fractions. For example, 8.99, 0.1 and etc. Double data type supports fractions with more decimal digits.
- String – Sequence of bytes enclosed by a pair of single or double quotes is called as PHP string. We can also use heredoc or nowdoc. But double quotes supports 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 same data type. These 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 external resource data. Example, file resource, database resource 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 which we can see with the syntax of PHP functions. These are used for representing 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 name, 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