PHP data types are used to express different data types that are associated with the variables or function return's type.
PHP variables are used to hold different data types such as Integer, Floating point number or Float, String, Boolean, Array, Object, resource, and NULL. The data types in PHP are classified into three categories.
There is 4 scalar data type in PHP and it contains a single value.
It can hold multiple data and it is categorized into two parts.
There are 2 special data types in PHP.
PHP integers are basically whole numbers without decimal points such as 1,2,3,-1,-2,0, etc.
Source Code
<?php
$a = 120;
// decimal number
var_dump($a);
$a = -123;
// a negative number
var_dump($a);
$a = 0x40;
// hexadecimal number
var_dump($a);
$a = 0300;
// octal number
var_dump($a);
$a = 0b10100000;
// binary number
var_dump($a);
?>
Floating point numbers ("floats", "doubles" or "real numbers") are numbers with a decimal point or a number in exponential form.
Source Code
<?php
$x = 24.365;
var_dump($x);
echo "<br />";
$x = 1.5e3;
var_dump($x);
?>
A string is a finite sequence of characters that is enclosed with single or double quotes. The string characters might be letters, numbers, special characters, symbols, etc.
There are four ways to represent a string. These are:
Source Code
<?php
$str = 'This is single quoted text';
$str = "This is double quoted text";
$str = <<<EOD
This is a heredoc syntax that is used
to represent string text
EOD;
$str = <<<'EOD'
This is a nowdoc syntax that
is used to represent string text
EOD;
var_dump($str);
?>
PHP array is a variable that holds multiple values in a single variable. There are two ways to represent an array:
[
and ]
. Source Code
<?php
$fruits = array('Apple', 'Banana', 'Orange', 'Mango');
var_dump($fruits);
?>
PHP object is basically is an instance of a user-defined class that has the ability to access instance variables and functions. Every object is completely independent of the other because they have their own properties and methods.
Source Code
<?php
// Class definition
class Resources{
// properties
public $learningResource = "sudhakarinfotech";
// methods
function displayResources(){
return $this->$learningResource;
}
}
// Create object from class
$obj = new Resources;
var_dump($obj);
?>
PHP NULL value is a special variable that has no value. It identifies whether a variable is empty or not. We can use the is_null()
function to check any value if it is NULL.
When a user has created a variable like $var
then a null
value is automatically assigned to it. Therefore, keep in mind that $var1=null
and $var2=""
is not same. Here $var1
represents null value while $var2
represent empty value.
Source Code
<?php
$a = NULL;
var_dump($a);
echo "<br>";
$b = "Hello World!";
$b = NULL;
var_dump($b);
?>
It is a special variable that holds the reference of other external resources. Basically, it belongs to the database function.
Source Code
<?php
$con = mysqli_connect("localhost","root","");
?>
gettype()
– data type getter returns given variables data types.
var_dump()
– This function is used to get the data type of an expression.
is_
– 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.