What are the 4 main Data Types in PHP?

What are the 4 main data types in PHP? PHP is a web-based application developing programming language, that can include HTML coding in it for building a web application. In PHP, there are 8 different data types that are used for representing and calling the variables in the script. They are ‘Integer’ for numeric values, ‘Boolean’ for true or false values, ‘String’ for characters, ‘Arrays’ for fixing element size, ‘Float/Double’ for decimal numerals, ‘object’ for representing instances of the class, ‘resources’ for referring to elements outside the PHP script and ‘NULL’ for the void.

Top 4 PHP Data Types

PHP variables used to save data may be connected with all sets of data types varying from the most simplistic int to more complex data types such as arrays. PHP has described as a loosely typed Programming language, which determines the variable data types are selected based on its attributes during run-time and is not explicitly described. It interprets the attributes of the value given and then defines the data type to be assigned to it. There are eight primitive data types that PHP holds and which can be further classified into 4 main types as mentioned below:

  1. Compound Types
  2. Special Types
  3.  Scalar Types
  4. Integral Types

Compound Data Types in PHP

These are the units for which new data cannot be assigned. Arrays and objects come under this category.

Arrays

It is a data structure having a set of fixed size of elements with related data types. It is also used to save the identified amount of key-value pairs in the form of an ordered map in it. It can be used for various purposes such as a list, collection, stack, dictionary, hash table, queue, etc, Multi-dimensional arrays are also possible.

A simple example of an array is as follows:

<?php
$colors = array("Black", "Brown", "Gray");
var_dump($colors);
$color_child = array(
"Black" => "black",
"Brown" => "brown",
"Gray" => "gray"
);
var_dump($color_child);
?>
Output
array(3) {
[0]=> string(5) “Black”
[1]=> string(5) “Brown”
[2]=> string(4) “Gray”
}
array(3) {
[“Black”]=> string(5) “black”
[“Brown”]=> string(5) “brown”
[“Gray”]=> string(4) “gray”
}

Objects

It provides to save data and also supplies data on how to process the methods of the object the same. An object assists as an instance of a class that is used as template for other objects. The keyword “new” is used for the production of an object.

Each object derives the properties and methods from that of the parent class. It needs an explicit declaration and a “class” in each object.

<?php
class className{
public $stmt = "Add any string here";
function show_statement(){
return $this->stmt;
}}
$msg = new className;
var_dump($msg);
?>
Output
object(className)#1 (1) {
[“stmt”]=> string(19) “Add any string here”
}

Special Data Types in PHP

There are two special data types in PHP that come under this category since they are unique. They are:

NULL

In PHP, this unique NULL is used for expressing empty variables in PHP i.e. the variable has no data in it and NULL is the only feasible value for it. A variable named to the constant NULL, if it has been set to unset() or if no value has been set to it displays a NULL data type.

Here we are introducing NULL directly to var1. Whereas, for the var2 variable, we are assigning a string value prime and then set it as NULL. In both cases the final value of variables is NULL.

<?php
$var1 = NULL;
var_dump($var1);
echo "<br>";
$var2 = "Any string";
$var2 = NULL;
var_dump($var2);
?>
Output
NULL
NULL

Resources

The resource is not a real data type although it is a special variable that holds a reference to a resource external to PHP. They hold special handlers for files and database links that are open. Specific functions usually create and use these resources.

To run this code, we need to have the name.txt built into the system with reading permission granted to it. It delivers an error in case “handle” is not a resource. Also, execute to connect to any current database in your system.

Aside from the superior data types, we also have something called pseudo-types which are the keywords in PHP documents used to show the types or values which an argument can have. Some of them are:

mixed: They provide a parameter to take more than one type. E.g: gettype()
number: With a number, a parameter can be a float or an integer.
callback, void, array|object are some of the other pseudo-types

Scalar Data Types in PHP

They can be further divided into primitive types as below:

Boolean

These types have their feasible output in the form of either 0 or 1 e.g. true or false. They are used for limited testing cases where the event returns true when the condition is satisfied and false when it does not satisfy. It also inspects NULL and empty string as false.

<?php
$variable_val = false;
var_dump($variable_val);
?>
Output
bool(false)

Float/ Double

The number having decimal point or an exponent is called a floating-point real number. It can have both positive and negative numbers. There shall be a pre-defined number of decimal places displayed for the number.

<?php
$decimal = 0.111;
var_dump($decimal);
$exp1 = 23.5e2;
var_dump($exp1);
$exp2 = 4E-9;
var_dump($exp2);
?>
Output
float(0.111)
float(2350)
float(4.0E-9)

Integral Data Types In PHP

The integral data type operates non-decimal total number values between -4,361,291,284 and 4,361,291,284. This maximum and minimum value depend on the system whether it is 32-bit or 64-bit. By using the regular PHP_INT_MAX we can find out the max value. Also operates base 10, base 8, and base 6 values.

<?php
// (base 10)
$dec1 = 200;
$dec2 = 400;
//(base 8)
$oct1 = 10;
// (base 6)
$hex1 = 0x15;
$putn = $dec1 + $dec2;
echo $putn;
?>
Output
600

Conclusion

Here we have closed almost all of the data types in PHP which are possible in PHP. All of the above eight primitive types are implicitly encouraged by PHP and there is no necessity for the user to define them manually. Arrays and objects can exist in many values although the rest can operate only a single value.