What are the Different Data Types in PHP and How to Use Them?

The values assigned to a PHP variable will be Different Data Types including numeric types and simple string to more complex data types as objects and arrays.
PHP data types define the type of data a variable can store, PHP supports a total of eight different types of data types Integer, Float, Array, String, Booleans, Object, resource, and NULL.
these PHP allows eight different types of data types are used to construct variable. let’s discuss all data types in detail, the first five data types are called simple data types and the last three data types are called the compound data types, or all eight different data types that can be categorized into three types
- Scalar Types (Predefined)
- Compound Types (User-defined)
- Special Types
Integers are classified as Whole Numbers.
Floating points are classified as Numbers with decimal Points.
Boolean is classified as True or false values.
Integer included whole numbers like as e.g. -2, -5, 0, 9, 32.
PHP Different Data Types: Classification Data Types Table
| Scalar Types | Compound Types | Special Types |
|---|---|---|
| The Scalar data types hold only a single value. There are 4 types of scalar data types in PHP. 1. boolean 2. integer 3. float 4. string |
The Compound Data Types hold only a single value. There are 2 types of Compound data Types in PHP. 1. array 2. object |
There are 2 types of Special data Types in PHP. 1. resource 2. NULL |
PHP Boolean
Booleans are the simplest data type that works like a switch. It holds only two values:- FALSE (0) or TRUE (1)
therefore, for unsuccessful events return false, and for Successful events will return true, means that If the condition is correct, then it will return TRUE otherwise FALSE. Booleans are used in a program with conditional statements.
If a string is empty then in boolean data type is considered as false.
In Boolean, NULL Type values are treated as false, Apart from NULL, 0 is also considered as false.
Example:
<?php if(TRUE) echo "This condition is TRUE"; if(FALSE) echo "This condition is not TRUE"; ?>
Output:
This condition is TRUE
PHP Integer
Integer holds only whole numbers (Numeric data) with a negative or positive sign. i.e., numbers without fractional part or decimal points.
An integer can be either positive (+) or negative (-) sign.
An integer must not contain a Decimal point or fractional part.
An integer must not contain a decimal point. Integer can be decimal (base 10), octal (base 8) or hexadecimal (base 16). by default, the base is decimal (base 10).
The octal (base 8) integers can be declared with leading 0 and the hexadecimal (base 16) can be declared with leading 0x.
Integers range must lie between -2^31 to 2^31.
Example:-
<?php // decimal base integers $deci1 = 50; $deci2 = 654; // octal base integers $octal1 = 09; // hexadecimal base integers $octal = 0x34; $sum = $deci1 + $deci2; echo $sum; ?>
output
704
PHP Double or PHP Float
it can hold numbers decimal part or fractional including negative numbers and positive numbers. the variable adds a minimum number of decimal places by default.
Example:-
<?php $val1 = 14.42; $val2 = 887.11; $sum = $val1 + $val2; echo $sum; ?>
Output:-
901.53
PHP String
A string is a non-numeric data type. It holds any alphabets or letters, numbers, and even special characters.
the string is written within double quotes during declaration but it will be treated differently while printing variables.
To clarify this see the given the example at below.
Example:-
<?php
$website = "tutorialscan";
//both single and double quote statements will treat different
echo "Wellcome $cwebsite";
echo "</br>";
echo 'Wellcome $website';
?>
Output:-
Wellcome tutorialscan Wellcome $website
PHP Array
An array is a compound data-type. the array can store multiple values of the same data type, Below is an example of an array of integers.
Example:-
<?php
$cars = array ("Hyundai", "Tata", "Maruti");
var_dump($cars); //the var_dump() function returns the datatype and values
echo "</br>";
echo "First Array Element: $cars[0] </br>";
echo "Second Array Element: $cars[1] </br>";
echo "third Array Element: $cars[2] </br>";
?>
Output:-
array(3) { [0]=> string(7) "Hyundai" [1]=> string(4) "Tata" [2]=> string(6) "Maruti" }
First Array Element: Hyunadai
second Array Element: Tata
third Array Element: Maruti
PHP NULL Value
therefore, it is a special type of variable that holds only one value i.e., NULL. here, We follow the convention of writing it in capital form, but its case sensitive.
Variables can also be emptied by setting the value to NULL:
Example:-
<?php $data = NULL; echo $data; // as result it's gives no output ?>
PHP object
Objects are the instances of user-defined classes that can hold both functions and values, They must be explicitly declared. I will be discussed this topic in detail in further articles.
Example:-
<?php
class car {
function brand() {
$brand_name = "Hyundai";
echo "Car Brand: " .$brand_name;
}
}
$obj = new car();
$obj -> brand();
?>
Output:-
Car Brand: Hyundai
PHP Resource
Resources are not an exact data type in PHP. These are basically used for the purpose to store references to external PHP resources or to some function call This is an external resource example consider that a database call.
A resource is a special variable, Resource variables typically hold special handlers database connections and to opened files. We will discuss in detail about resources further articles.
<?php
// Open a file for reading
$handle = fopen("data.txt", "r");
var_dump($handle);
echo "<br>";
// Connect to MySQL database server with default setting
$link = mysql_connect("localhost", "root", "");
var_dump($link);
?>
Predefine functions to Check a data type
is_int( ): For used to Check given value is integer or not
is_string( ): For used to Check given value is string or not
is_bool( ): For used to Check given value is Boolean or not
is_float( ): For Check given value is float or not
is_null( ): For Check given value is null or not
is_array( ): For used to Check given value is array or not
is_object( ): For used to Check given value is object or not
Question: Check if a given variable holds an integer type of value then print the sum otherwise show an error message?
Different data Types: Example
<?php
$a = 750;
$b = 850;
if(is_int($a) && is_int($b))
{
$sum = $a + $b;
echo "sum = ".$sum;
}
else
{
echo "both number must be an integer";
}
?>
Output:-
1600
Explanation:-
As you have seen in the given above the following example first Create two-variable $a which holds the value=100, and $y which hold the value=500, now execute if..else condition. then We pass is_int( ) function -inside if condition, this function to check the value is an integer or not. if yes then the statement is executed and print the sum of two values. otherwise, if not else statement is executed it shows an error message.