Php Variable Type Declaration And Type Casting In Php

Posted On : 19th Jun, 2019 - # Admin

In PHP, Variable declares with the $ (dollar) symbol. There is no need to define the data type of variable, PHP automatically set data type according to the variable's assigned value.


PHP Supports ten primitives of data types

  • Integer
  • Float
  • String
  • Boolean
  • Array
  • Object
  • Callable
  • Iterable
  • Resource
  • Null


An example of variable declaration


    /* Integer data type */
    $a = 123;

    /* Float data type */
    $b = 125.08;

    /* String data type */
    $string = "Hi testing program";

    /* Boolean data type */
    $d = false;

    /* Array data type */
    $e = array(1, 2, 3, 4);



Integer Data Type

  • An Integer data type is a numeric value.
  • It's the set of (.., -3, -2, -1, 0, 1, 2, 3, ...).
  • It can be a positive value and a negative value.
  • The data size of an integer value depends on the hardware platform.
  • An integer size on the 32-bit platform is 2147483647.
  • An integer size on the 64-bit platform is 9223372036854775807.


Float Data Type

  • A float number is a numeric value with exponent.
  • The size of the float data type depends on the platform.
  • Max size of float data type is 1.8e308.


String Data Type

  • A string data type is a set or series of characters.
  • PHP only supports 256-character set.
  • A string declares with four literals (single quoted, double quoted, heredoc syntax & nowdoc syntax).


Boolean Data Type

  • A Boolean is an expression value, It's the true value of a boolean expression.
  • It may be only either True or False.


Array Data Type

  • An array is a collection of many values more than one value.
  • An array of supports homogeneous data types.
  • Like It may be Integer, Float or string value.
  • This is the ordered map of values.
  • There are values are associated with keys.