Php Datatype

Php support 8 types of datatype. PHP variables behave according to the type of data they hold.

Integer
Float
String
Array
Object
Boolean
Null
Resources

Php Integer

Php Integer is a whole number means it is not decimal number.
Range of Integer variable value are from -2,147,483,648 to 2,147,483,647
Integer variable value can be positive or negative.

Example:- Suppose you have $num variable which has value 2500 and you can use var_dump() method to get datatype and variable value.


<?php
$num=2500;
var_dump($num);
?>
Output:- int(2500)

Try it Yourself

Php Float

Php floating-point number is also called real number. A floating number has decimal point and it has positive and negative sign before number.

Example:- Suppose you have $num variable which has value 2.5 and you can use var_dump() method to get datatype and variable value.


<?php
$num=2.5;
var_dump($num);
?>
Output:- float(2.5)

Try it Yourself

Php String

Php strings are sequences of characters (i.e. letters and numbers)
While adding a string value to your PHP code through single quotes or double quotes.

Single Quotes – This is the simplest option that you can use when creating a string.
Write your string between a pair of quotes.

Example:- Suppose you have $employee_name variable which has value John Taylor.


<?php
$employee_name='John Taylor';
echo $employee_name;
?>
Output:- John Taylor

Try it Yourself

Double Quotes – double quotes strings can hold the variable value.
Example:- Suppose you have $employee_name variable which has value John Taylor.


<?php
$employee_name='John Taylor';
$employee_greetings ="Hello $employee_name";
echo $employee_greetings;
?>
Output:- Hello John Taylor

Try it Yourself

Php Array

PHP array is a group of key and value pairs. array index can be integer or string. Phy array has multiple value into single variable.

Declare Php Array through array()
Syntax:-


<?php
$var=array();
?>

Example:- Suppose, You create a employee_name array which has multiple values and get the value.


<?php
$employee_name=array('John','Rom','Mathew');
echo $employee_name[0];  // John
echo $employee_name[1];  // Rom
echo $employee_name[2];  // Mathew
?>

Try it Yourself

Declare key and value into the array()
Suppose, You create a employee array which has keys and values and get the values based on the array key.


<?php
$employee=array('name'=>'John','age'=>5, 'designation'=>'Software Engineer');
// get the array element value
echo $employee['name'];
echo $employee['age'];
echo $employee['designation'];
?>

Try it Yourself

Php Object

Php Object is an instance of a class.
An entity that has state and behavior is known as an object like a car, cycle, table, chair, etc.

Example:- Suppose you create Employee class which has property name $empname and function display_empname() and after that create object through new keyword.


<?php
class Employee {
    public $empname;
    function display_empname($empname) {
        return $empname;
    }
 
}
$emp_obj = new Employee;
echo $emp_obj -> display_empname('John');
?>
Output:- John

Try it Yourself

Php Boolean

Php boolean is used in conditional codes like (if, loops, etc.) statements. You should know that data types are often converted to Boolean during runtime.

Php boolean has two state
1) true or 1
2) false or 0

Example:-


<?php
$a=true;
or
$b= false;
?>

Php NULL

Php NULL data type can only have a NULL value. You can use NULL datatype as a empty variables.

The isset() operator gives false output for Null values if the variable being checked exists while you use other data type then isset() will give you true if the variable exists .

Example:-


<?php
$x=NULL;
if(isset($x)){
echo 'success';

}else{
 echo 'fail';
}

?>
Output:- fail

Try it Yourself

Php Resource

PHP resource a special type of data. It represents an extension resource of PHP such as an open file.
You can not handle a resource variable directly.
You can simply send it to different functions that can interact with the involved resource.