Datatype are used to specify the type of variable according to the assigned value. PHP is loosely typed language so there is no need to provide data types at the time of variable declaration. In PHP data types are dived into three different types. They are,
1. Scalar Datatype
2. Compound Datatype
3. Special Datatype
- Scalar Datatype: All primitive data types comes under this category.
(i)Boolean:
This data type can store either true or false. In php value of true is 1 and false doesn’t contain any value.
<?php
$x=true;
echo $x;
?>
a) is_bool : This function is used to check whether the variable is boolean or not.
b) (bool) variable and ( boolean) variable : Boolean is an alias for bool. Both of them mean that same type of variable. It converts input as boolean variable.
<?php
$x=”true”;
$x=(bool) $x;
echo is_bool($x);
?>
(ii) Integer:
This data type is used to hold numeric values.
a) is_int and is_integer: These functions are used to check the input is integer or not.
b) (int) variable, (integer) variable and (intval) variable: They converts input variable as integer.
<?php
$x=”100″;
$x=(int)$x;
echo is_int($x);
?>
(iii) float:
This data type is used to hold decimal values.
a) is_float: This function is used to check the input is float or not.
b) (float) variable: This converts input variable as floating point numbers.
<?php
$x=”12.45″;
$x=(float)$x;
echo is_float$x;
?>
(iv) String:
String is collection of characters in php we can declared. In php strings can be declared in three ways. They are, (A) Using Single Quotations, (B) Using Double Quotations, (C) Using Heredoc Syntax
(A) Using Single Quotations:
<?php
echo ‘Welcome to Webnoid schools’;
?>
(B) Using Double Quotations:
<?php
echo”Welcome to Webnoid schools”;
?>
(C) Using Heredoc Syntax:
By using this syntax we can avoid the problem with single quotations and double quotations.
<?php
echo<<< my str;
<input type =’button’
value =”click”>
my str;
?> - Compound Data Types:
(i) Array: Array is a collection of elements.
(ii) Object: Object is an instance of class. - Special Data Types:
(i) Resource Data Type:
Resource data types refer the external resources like database connections, file pointers, ftp connections etc.
<?php
$x=mysql_connect(“localhost”,”root”,” “);
echo $x;
?>
(ii) Null Data Type:
In php Null gives not a value we can consider a variable as null variable based on three conditions.
1. If the variable is not set with any value.
2. The variable is set with Null value.
3. The value of variable is unset.
is_null: This function is used check whether the given input is null variable or not.
<?php
$x=100;
unset($x);
echo is_null($x);
?>