Types of Variables in PHP Explained Simply
Types of Variables in PHP
PHP is a server-side scripting language that uses variables to store data. Variables are like containers where you keep information you need to use later. In PHP, variables always start with a $ symbol. For example:
$name = "John";
$age = 25;
Main Types of Variables in PHP
1. String Variables
A string variable stores text, like names, words, or sentences.
$city = "London";
Here, $city holds the text London.
2. Integer Variables
An integer variable stores whole numbers (positive, negative, or zero).
$score = 100;
This means $score holds a numeric value without any decimal point.
3. Float (or Double) Variables
Float variables hold numbers with decimals.
$price = 99.99;
Here, $price contains a decimal value.
4. Boolean Variables
Boolean variables have only two possible values: true or false. These are useful for conditions.
$is_active = true;
This means $is_active is set to true.
5. Array Variables
An array stores multiple values in a single variable.
$fruits = array("Apple", "Banana", "Orange");
Here, $fruits holds three items in one variable.
6. Object Variables
Objects are variables that store data and functions together, created using classes.
class Car {
public $brand = "Toyota";
}
$myCar = new Car();
Here, $myCar is an object of the Car class.
7. NULL Variables
A NULL variable has no value assigned to it.
$address = NULL;
This means $address currently has no data.
Final Thoughts
When I understood these variable types, PHP became much easier to learn. Once you know what kind of data you are working with, coding in PHP feels more organized and fun.
