Overview of PHP Data Types
A Data Type is a type of data. The values that is assigned to the variables are of different data types that includes string, numeric types to array and objects type.
PHP supports eight primitive data types. They are
- Integers are whole numbers.
- They do not have decimal points.
- It includes positive and negative numbers both.
- We can specify integers in decimal (base 10), hexadecimal (base 16 – prefixed with 0x) or octal (base 8 – prefixed with 0)
<?php //decimal base integers $dec1 = 50; $dec2 = 80; //octal base integers $oct = 07; //hexadecimal base integers $hex = 0x45; $hex = 0x45; $sum = $dec1 + $dec2; echo $sum; ?>
PHP Floating point numbers
- Floating point numbers also known as float or double or real numbers.
- This are the decimal points or fractional numbers including positive and negative numbers.
<?php $a = 10.85; $b = 100.28; $sum = $a + $b; echo $sum; ?>
- A String is a collection of characters.
- It contains alphanumeric characters i.e. it can hold any alphabets as well as numbers
- Strings can be written in double quotes as well as in single quotes but it will be treated differently on execution.
<?php $a = "Mark"; echo "Hello $a \n"; echo 'Hello $a'; ?>
- Boolean holds only two values either true or false.
<?php $flag = TRUE; if($flag) echo "This condition is TRUE"; else echo "This condition is FALSE"; ?>
This condition is TRUE
- An Array is the data type which can hold several values of same data type at a time.
- The data values in the array are indexed.
<?php $a = array( 10, 20 , 30); echo "First Number: $a\n"; echo "Second Number: $a\n"; echo "Third Number: $a\n"; ?>
First Number: 10
Second Number: 20
Third Number: 30
An Object is a data type which holds both value and functions. It also stores data and provide information on how to process the data. Instances of every object is independent of its own methods and properties and hence can be manipulated.
Resources are not exactly data types but they basically store references to the external resources. For example, to open files or to make database call.
- NULL is the special variable that hold only NULL value that means empty variable.
- The NULL variable type doesn’t contain any data.
- It is case sensitive.
<?php $n = NULL; echo $n; // This will give no output ?>