PHP Data Types
PHP data type is used to hold the various types of data or value. There are three types of data types in PHP. It is a loosely data typed language, so there is no need to declare data type like int,float etc. InPHP,there are some function to check data type and its value.
- gettype(): It is used to check only data type.
- var_dump(): It is used to check the size and data type.
<?php $var = 60; echo "60 is: ".gettype($var); echo "<br>"; // ------------------------------ $var = 1000.56; echo "1000.56 is: ".gettype($var); echo "<br>"; // ------------------------------ $var = "PHP"; echo "PHP is: ".gettype($var) ?>Output 60 is: integer 1000.56 is: double PHP is: string Example2
<?php $num=100; $str="Hello PHP"; $f=3.40; var_dump($num); var_dump($str); var_dump($f); //var_dump($num,$str,$f); ?>Output int(100) string(9) "Hello PHP" float(3.4) Example of Array:
<?php $cars = array("You","Are","Good_Developer!"); var_dump($cars); ?>Output array(3) { [0]=> string(3) "You" [1]=> string(3) "Are" [2]=> string(14) "Good_Developer" } Example of Object.
<?php class Course { function Course() { $this->Course = "PHP"; } } // create an object $lang = new Course(); // show object properties echo $lang->Course; ?>Output PHP Example5.
<?php $x = "Hello PHP!"; $x = null; var_dump($x); ?>Output NULL Example: 6
<?php $con = mysqli_connect("localhost","root","","users"); ?>Note: It is used in database. Following are the predefine functions to Check data type
Predefine Data type | Description |
is_int( ) | To check the value is integer or not. |
is_float( ) | To check the value is float or not. |
is_numeric( ) | To check the value is either integer or float. |
is_string( ) | To check the value is string or not. |
is_bool( ) | To check the value is Boolean or not. |
is_object( ) | To check the value is object or not. |
is_null( ) | To check the value is null or not. |