PHP Data Types
Data Types define the type of data a variable can store information . PHP allows eight different types of data types.
- PHP supports the following data types:
- String
- Integer
- Float (floating point numbers - also called double)
- Boolean
- Array
- Object
- NULL
- Resource
PHP String
String Data type hold letters or any alphabets, even numbers are included. These are written within double quotes Or single quotes during declaration. The strings can also be written within single quotes
<?php
$a = "Hello Stack!";
$b = 'Hello Stack!';
echo $a;
echo "<br>";
echo $b;
?>
PHP Integer
Integers hold only whole numbers including positive and negative numbers, numbers without fractional part or decimal point. An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
Rules for integers data type:
- An integer must have at least one digit
- An integer must not have a decimal point
- An integers hold only whole numbers including positive and negative numbers
- An integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation
<?php
$j = 786786;
var_dump($j);
?>
PHP Float
Float data type is floating-point number is a number with a decimal point. Unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign.
<?php
$m = 12.786;
var_dump($m);
?>
PHP Boolean
Boolean Data type represents two possible states: TRUE or FALSE.
$i = true;
$j = false;
This data type we can often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.
PHP Array
This data type stores multiple values in one single variable.
<?php
$friends = array("Rohit","Ankit","Adil","Yaseen");
var_dump($friends);
?>
PHP Object
This data is defined as instances of user-defined classes that can hold both values and functions and information for data processing specific to the class.
When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
Let's assume we have a class named Mobile. A mobile can have properties like model, color, etc. We can define variables like $model, $color, and so on, to hold the values of these properties.
When the individual objects (Nokia, Samsung, Oppo, etc.) are created, they inherit all the properties and behaviors from the mobile class, but each object will have different values for the properties.
<?php
class Mobile {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function _message() {
return "My mobile phone is a ".$this->color." ".$this->model."!";
}
}
$myphone = new Mobile("black", "Nokia");
echo $myphone->_message();
echo "<br>";
$myphone = new Mobile("Golden", "iphone");
echo $myphone->_message();
?>
PHP NULL Value
NULL data type special types of variables that can hold only one value. A variable of data type NULL is a variable that has no value assigned to it.
<?php
$am = "Hello Stack!";
$am = null;
var_dump($am);
?>
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. For example - a database call. It is an external resource.
This is an advanced topic of PHP, so we will discuss it later in detail with examples.
The PHP var_dump() function returns the data type and value.By doing this function, we can check that the type of the value we have defined in the variable is