PHP Data Types Example
In this article, we will see PHP data types with example.
What are data types?
The name itself indicates, data type means a type of data.
Types of data types
PHP data types are mainly divided into 3 types:
- Scalar Data Types: integer, double, string, boolean
- Composite Data Types: array, object
- Special Data Types: NULL, resource
Scalar Data Types
It allows us to create variables or constants ; which can hold a single value
1. Integer
Integer means whole number, i.e number without floating point. Ex : 4, 53, 32 etc.
$playerSpeed = 10; // playerscore variable is holding integer type of data
2. Double
It means decimal or float number, i.e. number containing floating point
Ex : 3.142, 9.8, etc.
$playerSpeed = 3.5; // playerscore variable is holding double type of data
3. String
It indicates a sequence o characters enclosed within pair of double quotations or single quotations. i.e textual data
Ex: “Hello World”, “Hello PHP” etc.
$playerSpeed = "Mr.John"; // playerscore variable is holding string type of data
4. Boolean
It indicates the logical or conditional results. Boolean data types have two values true and false value.
Note: Integers 0 and -0 , double 0.0 nad -0.0, string with value “0”, boolean false value, an array with zero elements, the special type NULL and XML objects crated form empty tags ,a re considered as false in PHP
Ex: $isGameOver = true // isGameOver variable is holding boolean type of data
Composite Data Types
Allow us to create variables or constants; which can hold multiple values.
composite types are composed of scalar, composite and/or special-type data values.
Ex: Arrays and objects
$names = array("rama", "ravi"); // names variable is holding array type of data
$point1 = new point(); // point variable is holding objects type of data
Special Data Types
It allows us to make a variable point to some external resource or not print anywhere.
1. NULL:
Null indicates nothing. A variable of types object points or refers to another memory location; if it should not point to any memory location then we assign a value null to it .
Ex: $point1 = null; // point1 variable is holding NULL type of data
2. resource
It indicates variable is pointing to an external resource
Ex: $file = fopen("index.php", "r"); // file variable is holding resource type of data