Data Types define the type of data a variable can store.
PHP supports 8 primitive data types that can be categorized further in 3 types.
- Scalar Types (predefined)
- Compound Types (user-defined)
- Special Types
Scalar Types
It can hold only single value.
Boolean
It holds only two values: TRUE (1) or FALSE (0).
Booleans are like a switch it has only two possible values either 1
(true) or 0
(false). It is often used with conditional statements.
Example
<?php
if (TRUE)
echo "This condition is TRUE.";
if (FALSE)
echo "This condition is FALSE.";
?>
Integer
Integer means numeric data with a negative or positive sign.
Integer can be decimal (base 10), octal (base 8), or hexadecimal (base 16). It holds only whole numbers, i.e., numbers without fractional part or decimal points.
Example
<?php
$dec1 = 34;
$oct1 = 0243;
$hexa1 = 0x45;
echo "Decimal number: " .$dec1. "</br>";
echo "Octal number: " .$oct1. "</br>";
echo "HexaDecimal number: " .$hexa1. "</br>";
?>
The PHP var_dump() function returns the data type and value.
<?php
$x = 5985;
var_dump($x);
?>
Float
A floating-point number is a number with a decimal point.
Example
<?php
$n1 = 19.34;
$n2 = 54.472;
$sum = $n1 + $n2;
echo "Addition of floating numbers: " .$sum;
?>
String
A string is a sequence of characters, like “Hello world!”.
A string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special characters.
String values must be enclosed either within single quotes or in double quotes. But both are treated differently.
Example
<?php
$company = "EyWiah.com";
//both single and double quote statements will treat different
echo "Hello $company";
echo "</br>";
echo 'Hello $company';
?>
Single quoted strings will display things almost completely “as is.” Variables and most escape sequences will not be interpreted.
Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated.
Compound Types
It can hold multiple values. There are 2 compound data types in PHP.
PHP Array
An array stores multiple values in one single variable.
An array is a compound data type.
Example
<?php
$bikes = array ("Royal Enfield", "Yamaha", "KTM");
var_dump($bikes); //the var_dump() function returns the datatype and values
echo "</br>";
echo "Array Element1: $bikes[0] </br>";
echo "Array Element2: $bikes[1] </br>";
echo "Array Element3: $bikes[2] </br>";
?>
You will learn more about array in later chapters of this tutorial.
PHP object
Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared.
Classes and objects are the two main aspects of object-oriented programming.
Example
<?php
class bike {
function model() {
$model_name = "Royal Enfield";
echo "Bike Model: " .$model_name;
}
}
$obj = new bike();
$obj -> model();
?>
Special Types
There are 2 special data types in PHP.
PHP Resource
Resources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources.
We will discuss resources in detail in further articles.
Example
A database call. It is an external resource.
PHP Null
Null is a special data type that has only one value: NULL.
If a variable is created without a value, it is automatically assigned a value of NULL.
A variable of data type NULL is a variable that has no value assigned to it.
Variables can also be emptied by setting the value to NULL.
A variable of type NULL is a variable without any data.
Example
<?php
$nl = NULL;
echo $nl; //it will not give any output
?>
You will learn a lot more about arrays, objects and resources in later chapters of this tutorial.