Latest posts

10/recent/ticker-posts

Data Types in PHP | Types of Data types in PHP | gettype() Function in PHP

The data type is used to define which type of variable is stored in memory.

1. Integer - It is the scalar data type. It is used to store and represent the integer value, but it can not store floating value. in this data, the type number is positive or negative.
example,
<?php
    $a=100;
    echo "a is : " .$a. "\n";
    echo "type of a is: ".gettype($a);
?>


in this example, the output will be displayed as "a is: 100" and "type of a is: double".

2. Float - Float is the scalar data type. It is used to store and represent the floating value(fractional or decimal), but it can not store the floating value. in floating, the data type given number is positive or negative.  the floating data type is also known as double.
example,
<?php
    $a=1.5;
    echo "a is : " .$a. "\n";
    echo "type of a is: ".gettype($a);
?>


in this example, the output will be displayed as "a is: 1.5" and "type of a is: double".

3. String - String is the scalar data type. It stores and represents characters, alphabets, and numeric values. The string is a collection of sets of characters. the string can be written into single quotes (' ') and double quotes(" ").
example,
<?php
    $a="computer";
    echo "$a \n";
    echo "type of a is: ".gettype($a);
?>


in this example, the output will be displayed as "a is: computer" and "type of a is: double".

4. Boolean - it is the scalar data type. in Boolean data type there are two TRUE and FALSE. condition is true it will return true and if the condition is false it will return false. 
example,
<?php
    if(TRUE)
    echo "condition is true";
    if(FALSE)echo "condition is true";
?>


in this example, the output will be displayed as "condition is true".

5. NULL -  NULL is the unique data type.in NULL data type variables are created without value. it means values are not assigned to the variable. 
example,
<?php
    $a=NULL;
    echo "type of a is: ".gettype($a);
?>


in this example, the output will show "type of a is: NULL".


6. Array - it is the compound data type. An array is the collection of the data type of an element.
example,
<?php
    $a=array("a", "b" ,"c ","d");
    echo "type of a is: ".gettype($a)."\n";
    echo "the first element is:".$a[0]."\n";
    echo "the second element is:".$a[1]."\n";
    echo "the third element is:".$a[2]."\n";
    echo "the forth element is:".$a[3];
?>

// in this example, the output will be shown as"
// type of a is: Array
// the first element is: a
// the second element is: b
// the third element is: c
// the fourth element is d".


So these are the type of data types in PHP, we hope it will help you to learn PHP More...!

Thank You...!!!




Also Visit


Post a Comment

0 Comments