Course Banner

PHP Data Types

Before we proceed for knowing about out data types, let’sbear in mind that PHP is a loosely typed language. Java, C, C++ etc. are strongly typed language. The later programing langue are called strongly typed language because we must define data type before using it. And we can store value in variable only as defined data type. PHP is called loosely typed language because we don’t specifically define data type for PHP variables. So, if we define a variable say $a; we can store either number or string both. PHP automatically converts it to required data type.

Example :

  1. $a=30;
  2.       echo $a;
  3. Result : 30;
  4. $a="Hemant Kumar";
  5.       echo $a;
  6. result:
  7. hemant Kumar
  8. ?>

From the above example, you can understand that same variable a can store integer value as well as string also. So, whether you call it strength or weakness, PHP is like that and it’s a magical power for PHP also In spite of that PHP supports data various data types where we can specifically define data type.

PHP supports following data types:

  • ● String

String is a list of characters. Like "I am Indian";

Example :

  1. $name =”Here is my name”;
  2. ?>
  • ● Integer Integer is whole numbers like 1, 2,3 etc. PHP supports integer data types also.
  • ● An integer must have at least one digit (0-9).
  • ● Integer cannot contain comma or space.
  • ● An integer must be a whole number and not a decimal.
  • ● Integer may be positive or negative.

If you want to see data type, you can apply var_dump();

Example :

  1. $a = 123;
  2. var_dump($a);
  3. ?>

Output :

  • ● Float

Floating point numbers are those which has decimal numbers "floats", "doubles” comes under this.

Example :

  1. $a = 1.234;
  2. $b = 1.80798798798;
  3. var_dump($b);
  4. ?>

And the output :

  • ● Boolean

This is the easiest type. A Boolean expresses a truth value. It can be either TRUE or FALSE.

Example :

  1. $a = true;
  2. $b = false;
  3. ?>
  • ● Array Data Types

Array is a group of objects which has common name and its elements is identified by its index.


  1. $array = array('mango','banana','apple');
  2. var_dump($array);
  3. ?>
  • ● Object

An object is a collection of data and code. To initialize an object, you use the new statement to instantiate the object to a variable.

Example :

  1. classfoo
  2. {
  3. functiondo_foo()
  4. {
  5.       echo"I am inside class function.";
  6. }
  7. }
  8. $bar = newfoo;
  9. $bar->do_foo();
  10. ?>

The output:

  • ● Null

Null data types denotes that the variable contains NULL value. NULL is the only possible value of type NULL. A variable is considered to be NULL if it has been assigned the constant NULL. it has not been set to any value yet. it has been unset().

Syntax :
That is the case insensitive keyword NULL.

  1. $var = NULL;
  2. ?>
  • ● Resource

Resource Data Types A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions