Data Types define the type of data a variable can store. PHP allows eight different types of data types. All of them are discussed below. There are pre-defined, user-defined, and special data types.
The predefined data types are:
- Boolean
- Integer
- Double
- String
The user-defined (compound) data types are:
- Array
- Objects
The special data types are:
- NULL
- resource
The first five are called simple data types and the last three are compound data types:
1. Integer: Integers hold only whole numbers including positive and negative numbers, i.e., numbers without fractional part or decimal point. They can be decimal (base 10), octal (base 8), or hexadecimal (base 16). The default base is decimal (base 10). The octal integers can be declared with leading 0 and the hexadecimal can be declared with leading 0x. The range of integers must lie between -2^31 to 2^31.
Example:
PHP
<?php // decimal base integers $deci1 = 50; $deci2 = 654; // octal base integers $octal1 = 07; // hexadecimal base integers $octal = 0x45; $sum = $deci1 + $deci2 ; echo $sum ; echo "\n\n" ; //returns data type and value var_dump( $sum ) ?> |
704 int(704)
2. Double: Can hold numbers containing fractional or decimal parts including positive and negative numbers or a number in exponential form. By default, the variables add a minimum number of decimal places. The Double data type is the same as a float as floating-point numbers or real numbers.
Example:
PHP
<?php $val1 = 50.85; $val2 = 654.26; $sum = $val1 + $val2 ; echo $sum ; echo "\n\n" ; //returns data type and value var_dump( $sum ) ?> |
705.11 float(705.11)
3. String: Hold letters or any alphabets, even numbers are included. These are written within double quotes during declaration. The strings can also be written within single quotes, but they will be treated differently while printing variables. To clarify this look at the example below.
Example:
PHP
<?php $name = "Krishna" ; echo "The name of the Geek is $name \n" ; echo 'The name of the geek is $name ' ; echo "\n\n" ; //returns data type, size and value var_dump( $name ) ?> |
The name of the Geek is Krishna The name of the geek is $name string(7) "Krishna"
4. Boolean: Boolean data types are used in conditional testing. Hold only two values, either TRUE(1) or FALSE(0). Successful events will return true and unsuccessful events return false. NULL type values are also treated as false in Boolean. Apart from NULL, 0 is also considered false in boolean. If a string is empty then it is also considered false in boolean data type.
Example:
PHP
<?php if (TRUE) echo "This condition is TRUE" ; if (FALSE) echo "This condition is not TRUE" ; ?> |
This condition is TRUE
5. Array: Array is a compound data type that can store multiple values of the same data type. Below is an example of an array of integers. It combines a series of data that are related together.
PHP
<?php $intArray = array ( 10, 20 , 30); echo "First Element: $intArray[0]\n" ; echo "Second Element: $intArray[1]\n" ; echo "Third Element: $intArray[2]\n\n" ; //returns data type and value var_dump( $intArray ); ?> |
First Element: 10 Second Element: 20 Third Element: 30 array(3) { [0]=> int(10) [1]=> int(20) [2]=> int(30) }
We will discuss arrays in detail in further articles.
6. Objects: Objects are defined as instances of user-defined classes that can hold both values and functions and information for data processing specific to the class. This is an advanced topic and will be discussed in detail in further articles. When the objects are created, they inherit all the properties and behaviours from the class, having different values for all the properties.
Objects are explicitly declared and created from the new keyword.
PHP
<?php class gfg { var $message ; function gfg( $message ) { $this ->message = $message ; } function msg() { return "This is an example of " . $this ->message . "!" ; } } // instantiating a object $newObj = new gfg( "Object Data Type" ); echo $newObj -> msg(); ?> |
This is an example of Object Data Type!
7. NULL: These are special types of variables that can hold only one value i.e., NULL. We follow the convention of writing it in capital form, but it’s case-sensitive. If a variable is created without a value or no value, it is automatically assigned a value of NULL. It is written in capital letters.
Example:
PHP
<?php $nm = NULL; echo $nm ; // this will return no output // return data type var_dump( $nm ); ?> |
NULL
8. Resources: Resources in PHP are not an exact data type. These are basically used to store references to some function call or to external PHP resources. For example, consider a database call. This is an external resource. Resource variables hold special handles for files and database connections.
We will discuss resources in detail in further articles.
Note:
- To check the type and value of an expression, use the var_dump() function which dumps information about a variable.
- PHP allows the developer to cast the data type.
This article is contributed by Chinmoy Lenka. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.