Data types 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, 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 is used to store and represent 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 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, output will be displayed as "condition is true".
5.NULL - NULL is the special data type.in NULL data type variables are creating without value. it means value are not assign to the variable.
example,
<?php
$a=NULL;
echo "type of a is: ".gettype($a);
?>
in this example, output will be show "type of a is: NULL".
6.Array - it is the compound data type. Array is the collection of data type of 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, output will be show as"
type of a is: array
the first element is: a
the second element is: b
the third element is: c
the forth element is: d".
No comments:
Post a Comment