A PHP data types is the classification of data into a category according to its attributes.
PHP is a loosely typed language, the type of a variable is explicitly set by programmer.
PHP determines the data type at runtime depending on attributes of data supplied.
var_dump is function returns the data type and value of variable.
PHP supports 10 primitive data types that categorized in 3 types:
- Scalar types
- Compound types
- Special types
Scalar PHP data types
PHP supports following 4 scalar data types:
- integer
- float(floating-point number, aka double)
- string
- boolean
Compound types
PHP supports following 4 compound data types:
- array
- object
- callable
- iterable
Special types
PHP supports following 2 special data types:
- resource
- NULL
Now following is a brief description of each data type with examples.
Integer
Integer is number of the set {…,-3,-2,-1,0,1,2,3….}.
Integer can be specified in:
- binary
- decimal
- hexadecimal
- octal
Binary integer literals are available since PHP 5.4.0.
To use number as hexadecimal(base 16) prefix number with 0x.
To use number as octal(base 8) prefix number with 0.
For using number as binary(base 2) prefix number with 0b.
Example
1 2 3 4 |
<?php $x = 10; //decimal number $y = -10; //negative number ?> |
PHP does not support unsigned integers.
The size of integer is platform-dependent.
A constant PHP_INT_SIZE is used to determine the integer size.
The constant PHP_INT_MAX is used to determine the maximum value.
The constant PHP_INT_MIN is used to determine the minimum value.
Example
1 2 3 4 |
<?php $a = 834; var_dump($a); // int(834) ?> |
Float
Floating-point numbers are decimal numbers. They are also known as double or real numbers.
The size of float is platform dependent.
Example
1 2 3 4 |
<?php $a = 3.15; var_dump($a); // float(3.15) ?> |
String
String is a series of characters.
A string literals can be specified within any single or double quotes.
Example
1 2 3 4 5 6 |
<?php $a = "Web Encyclop"; $b = 'Web Encyclop'; var_dump($a); // string(12) "Web Encyclop" var_dump($b); // string(12) "Web Encyclop" ?> |
Booleans
This is a simplest data type.
Boolean data type returns true or false.
True or false are case-insensitive.
Boolean data type is used to test conditions.
‘==’ is operator use to test condition which returns boolean.
1 2 3 4 |
<?php $a = True; $b = False; ?> |
Array
Arrays give you the ability to store not just one value inside a variable, but a whole bunch of values in a single variable.
Following example shows how to declare array data type.
1 2 3 |
<?php $zoo = array("Tiger", "elephant", "snake"); ?> |
We will discuss array in detail in our separate chapter array of this tutorial.
NULL
NULL value represents a variable with no value.
A variable is considered to be null if it is assigned the constant NULL or it has not set any value.
1 2 3 |
<?php $a = NULL; ?> |