How to Use Variables and Data Types in PHP (A Clear Guide for Absolute Beginners) Part 2
In the second part of this guide, we will see more PHP types.
Also, we will see how to check a variable type and how to change the type of a PHP variable.
Type 5 - Array
The array is a collection of values.
$langs = ["php", "java", "python"];
Type 6 - Object
The object is an instance of a class.
class User {
public $age = 18;
}
$user = new User();
echo $user->age; //18
Type 7 - NULL
The null is a variable with no value.
$empty = null;
How to check a variable type
To check a variable type, use the gettype() function.
$name = "john doe";
echo gettype($name ); //string
How to change the type of a PHP variable
To change the data type of a PHP variable, see the example below.
$age = "18"; // string
$age = (int)$age; // cast to integer