Learn PHP Tutorial

PHP Data Types

Data type specifies which type of value a variable has. During PHP program variables are used to store informations.

PHP supports following data types to construct our variables –

  • Integer
  • Float
  • String
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

1. PHP Integers – A PHP Integer data type is a whole number (non-decimal numbers).

  • An Integer ranges is -2,147,483,648 and 2,147,483,647.
  • An Ineger must have at least one digit.
  • An Integer must not have a decimal point.
  • An Integer can be positive or negative.
  • Integers can be decimal (base 10), hexadecimal (base 16) format.
  • Default format is Decimal.

To check the datatype and value of a variable, a function var_dump() is used.

$x = 4296;

2. PHP Floats – A float is a decimal point number or an exponential form number.

For example –
33.303, 0.93993, -9390240.93493 are floating point numbers.

$x = 93933.39;

3.PHP Boolean – Boolean data type is any data type having only true or false value, yes or no, on or off (0 or 1).

  • By default, the boolean data type is set to false.
  • Boolean data type is used for condition testing.

For example
$x = true;
$y = false;

4. PHP Array – An array is a container which holds multiple values of a single type.
For example –

$subjects = array(“English”, “Hindi”, “Math”);

5. PHP Object – An object is an instance of a class which can be containing variables, functions and data structures.
An object data type variable stores data and information.

$var1 = NULL;
$var2 = "";

Both $var1 and $var2 are two different values containing variables. $var1 has null value whereas $var2 has to assigned some values.

6. PHP resources – Resource is a special type of datatype which does not store any actual data but stores reference to some external source of data/information.

For example-
A very common example of it is Database call.

Class Car{
Function car(){
$this->model = “BMW”;

//Create an object
$newcar = new Car();
//object properties
Echo $newcar->model;

7. PHP NULL – PHP Null values is used to represent only empty values. Any variable of NULL type is without any data. And its only value is NULL.
Whenever you create a variable like $car with no value in it. It is automatically assigned NULL