Types Of Datatypes

Datatype are used to specify the type of variable according to the assigned value. PHP is loosely typed language so there is no need to provide data types at the time of variable declaration. In PHP data types are dived into three different types. They are,
1. Scalar Datatype
2. Compound Datatype
3. Special Datatype

  1. Scalar Datatype: All primitive data types comes under this category.
    This data type can store either true or false. In php value of true is 1 and false doesn’t contain any value.
    echo $x;
     a)   is_bool : This function  is used to check whether the variable is boolean or not.
      b)  (bool) variable and ( boolean) variable : Boolean is an alias for bool. Both of them mean that same type of variable. It converts input as boolean variable.
    $x=(bool) $x;
    echo is_bool($x);
    (ii) Integer:
    This data type is used to hold numeric values.
     a) is_int and is_integer: These functions are used to check the input is integer or not.
     b) (int) variable, (integer) variable and (intval) variable: They  converts input variable as integer.
    echo is_int($x);
    (iii) float: 
    This data type is used to hold decimal values.
    a) is_float: This function is used to check the input is float or not.
    b) (float) variable: This converts input variable as floating point numbers.
    echo is_float$x;
    (iv) String:
    String is collection of characters in php we can declared. In php strings can be declared in three ways. They are, (A) Using Single Quotations, (B) Using Double Quotations, (C) Using Heredoc Syntax
    (A) Using Single Quotations:
    echo ‘Welcome to Webnoid schools’;
    (B) Using Double Quotations:
    echo”Welcome to Webnoid schools”;
    (C) Using Heredoc Syntax:
    By using this syntax we can avoid the problem with single quotations and double quotations.
    echo<<< my str;
    <input type =’button’
    value =”click”>
    my str;
  2. Compound Data Types:
    (i) Array: Array is a collection of elements.
    (ii) Object:  Object is an instance of class.
  3. Special Data Types:
    (i) Resource Data Type:
    Resource data types refer the external resources like database connections, file pointers, ftp connections etc.
    $x=mysql_connect(“localhost”,”root”,” “);
    echo $x;
    (ii) Null Data Type:
    In php Null gives not a value we can consider a variable as null variable based on three conditions.
    1. If the variable is not set with any value.
    2. The variable is set with Null value.
    3. The value of variable is unset.
    is_null: This function is used check whether the given input is null variable or not.
    echo is_null($x);

Leave a Reply

Your email address will not be published. Required fields are marked *