Introduction to PHP Data Types
PHP or Hypertext PreProcessor is a web-based application development programming language that can incorporate HTML coding in them for constructing a web application. In PHP, there are eight different data types that are used for declaring and calling the variables in the script. They are ‘Boolean’ for true or false values, ‘Integer’ for numeric values, ‘Float/Double’ for decimal numerals, ‘String’ for characters, ‘Arrays’ for fixing element size, ‘object’ for representing instances of the class, ‘NULL’ for a void, and ‘resources’ for referring to elements outside the PHP script.
Top 3 PHP Data Types
PHP variables used to store values may be associated with all kinds of data types ranging from the simplest int to more complicated data types such as arrays. PHP is called a loosely typed Programming language, which means the variable data types are decided based on their attributes during run-time and is not explicitly defined. It analyses the value-given attributes and then determines the data type to be assigned to it. There are 8 primitive data types which PHP supports and which can be further classified into 3 types as below:
Let us go through each one of them in detail with an example each.
1. Scalar Types
They can be further divided into primitive types as below:
These types have their possible output in the form of either 0 or 1, i.e. true or false. They are used for conditional testing cases where the event returns true when the condition is satisfied and false when it does not satisfy. It also considers NULL and empty string as false.
// TRUE is assigned to a variable value
$variable_value = true;
An integer data type holds non-decimal whole number values between -2,147,483,648 and 2,147,483,647. This maximum and minimum value depend on the system, whether it is 32-bit or 64-bit. By using the constant PHP_INT_MAX, we can find out the max value. It also holds base 10, base 8 and base 6 values.
// example for decimal (base 10)
$dec1 = 100;
$dec2 = 200;
// example for decimal (base 8)
$oct1 = 10;
// example for decimal (base 6)
$hex1 = 0x15;
$addn = $dec1 + $dec2;
c. Float/ Double
A number having a decimal point or an exponent is called a floating-point number/ real number. It can have both positive and negative numbers. There shall be a pre-defined number of decimal places displayed for the number.
$dec1 = 0.134;
$exp1 = 23.3e2;
$exp2 = 6E-9;
A string data type is basically a collection of characters, including numbers, alphabets, and letters. They can hold values up to 2GB. They are to be declared using double quotes if a variable has to be displayed amongst the string. Else, a single quote also works.
$name = "Jay";
$str1 = 'Declaring name in single quote as $name';
$str2 = "Declaring name in double quote as $name";
$str3 = 'Just a string';
2. Compound Types
These are the ones for which new values cannot be assigned. Arrays and objects fall under this category.
It is a data structure having a collection of a fixed size of elements with similar data types. It is also used to store the known amount of key-value pairs in the form of an ordered map in it. It can be used for various purposes like a list, hash table (map implementation), collection, stack, dictionary, queue, etc.; multi-dimensional arrays are also possible.
A simple example of an array is as follows:
$animals = array("Dog", "Cat", "Cow");
$animal_babies = array(
"Dog" => "Puppy",
"Cat" => "Kitten",
"Cow" => "Calf"
It allows storing data (called its properties) and gives information on how to process (called the methods of the object) the same. An object serves as an instance of a class that is used as templates for other objects. The keyword “new” is used for the creation of an object.
Each object inherits the properties and methods from that of the parent class. It requires an explicit declaration and a “class” in each object.
// Declaring a class
public $stmt = "Insert any string here";
// Declaring a method
// Creation of new object
$msg = new statement;
3. Special Types
There are 2 special data types in PHP which fall under this category since they are unique. They are:
In PHP, this special NULL is used for representing empty variables, i.e. the variable has no data in it, and NULL is the only possible value to it. If it has been set to unset() or if no value has been set to it, a variable assigned to the constant NULL becomes a NULL data type.
Here we are setting NULL directly to val1. For the val2 variable, we are assigning a string value first and then setting it as NULL. In both cases, the final value of variables is NULL.
$val1 = NULL;
$val2 = "Any string";
$val2 = NULL;
A resource is not an actual data type, whereas it is a special variable that keeps a reference to a resource external to PHP. They hold special handlers for files and database connections that are open. Special functions usually create and use these resources.
To run this code, we must have the file.txt created in the system with reading permission given to it. It throws an error in case “handle” is not a resource. Also, make sure to connect to any existing database in your system.
// Open an existing file to read
$handle = fopen("file.txt", "r");
// Connecting to MySQL database server with settings set to default
$db = mysql_connect("localhost", "root", "");
Apart from the above data types, we also have something called pseudo-types: the keywords in PHP document used to indicate the types or values that an argument can have. Some of them are:
- mixed: They allow a parameter to accept more than one type. Ex: gettype()
- number: With a number, a parameter can be afloat or an integer.
- void, callback, array|object are some of the other pseudo-types
Here we have covered almost all of the data types which are available in PHP. All of the above 8 primitive types are implicitly supported by PHP, and there is no need for the user to specify them manually. Arrays and objects can hold multiple values, whereas, for rest, all can hold only a single value (except NULL, which holds no value).
This is a guide to PHP Data Types. Here we discuss the top 3 PHP data types such as scalar, compound, and special in detail, along with examples and implementation. You may also look at the following articles to learn more-