PHP Data Types



PHP Data Types :

PHP has a total of eight data types which we use to construct our variables:

Type Example Description
Integer 15 A whole number without decimal point
Double 3.234 A floating point number having decimal point
String hello php A collection of characters
Boolean true Having value true or false
NULL NULL Special kind that has single value i.e NULL
Arrays - Collection of the values
Object - Instances of programmer-defined classes
Resources DB Connection Reference to a third-party resource

1. PHP Integer Data Type

In PHP any number without decimal is called as integer.

Rules for integers:

  1. An integer must have at least one digit from 0 to 9
  2. An integer must not have comma,blanks or special symbol
  3. An integer must not have a decimal point
  4. An integer can be either positive or negative

Formats of Integer :

Integers are specified in three formats :

Format Example Explanation
decimal (10-based) 10 Normal Integer
hexadecimal (16-based) 0x12 Prefixed with 0x
octal (8-based) 012 Prefixed with 0

Program to Display type of Integer and Value

<?php 
$num = 123;
var_dump($num);
echo "<br />"; 

$num = -123; 	// negative number 
var_dump($num);
echo "<br />"; 

$num = 0x8C; 	// hexadecimal number
var_dump($num);
echo "<br />";

$num = 047; 	// octal number
var_dump($num);
?>

2. PHP Floating Data Type

A floating point number is a number which is having decimal point or a number in exponential form.

Formats of Floating :

floating point are specified in three formats :

Format Example Explanation
Decimal point 10.10 Simple example of floating point
Exponential Form(e) 1.2e3 Example of floating point having positive exponential
Exponential Form(e) 2E0-3 Example of floating point having negative exponential

Example :

<?php 
$num = 10.10;
var_dump($num);
echo "<br>";
 
$num = 1.2e3;
var_dump($num);
echo "<br>"; 

$num = 2E-3;
var_dump($num);
?>

3. PHP String Data Type

String is the collection of the characters. Characters may be alphabets or digits or any type of special symbols.

Formats of String :

String can be written in two formats :

Format Example
Inside double quotes Hello PHP
Inside Single quotes 'Hello PHP'

Example :

<?php 
$str = "Hello PHP";
echo $str;
echo "<br>";
 
$str = 'Hello PHP';
echo $str;
?>

4. PHP Boolean Data Type

Booleans can be either TRUE or FALSE.

$x = true;
$y = false;

Boolean values are usually used for testing the condition. We can use Boolean values inside the if statement.

5. PHP Array Data Type

In PHP array is collection of the multiple values under single variable name

Example :

We are creating an array of the days of the month -

<?php 
$months = array("jan","feb","march");
var_dump($cars);
?>

6. PHP Object Data Type

  1. Objects are the special instances of the classes that are created by user.
  2. Classes can be user defined or in-build.
  3. Class keyword is used to declare an object.
  4. Class is special kind of structure which has properties and methods
  5. In PHP, we need to declare an object explicitly.
<?php
class Human
{
  var $name;
  function Human($name = "sana") 
  {
    $this->name = $name;
  }
  function getName() 
  {
    return $this->name;
  }
}
?>

7. PHP NULL Data Type

  1. Uninitialized variable takes value NULL in PHP.
  2. It represents that variable has no value.
  3. NULL is the only possible value of data type NULL.
  4. If the variable is having NULL value then we can say that variable is empty.
  5. We can also make variable empty by setting the value to NULL

Example :

<?php
$str = "Hello world!";
$str = null;
var_dump($str);
?>

B. Displaying the Type of a Variable

<?php
$test_var; // declare without assigning
print gettype( $test_var ); // NULL
print "<br />";

$test_var = 5;
print gettype( $test_var ); // integer
print "<br />";

$test_var = "five";
print gettype( $test_var ); // string
print "<br />";

$test_var = 5.0;
print gettype( $test_var ); // double
print "<br />";

$test_var = true;
print gettype( $test_var ); // boolean
print "<br />";
?>