Variables, Data Types, and Constants
When you start writing real PHP programs, the first thing you need to understand is how data is stored and managed. PHP uses variables to store values, data types to define what kind of data is being handled, and constants for values that should never change.
In this lesson, you’ll learn how PHP variables work, the different data types available, and how to define constants properly. By the end, you’ll be able to store, inspect, and manage data in your PHP applications with confidence.
What is a Variable in PHP?
A variable in PHP is used to store data that your program can use and modify during execution. For example, when a user logs into a website, their name or email is stored in variables so your application can process and display that information.
Syntax
$name = "Rohan"; $age = 22;
In PHP, every variable must begin with a dollar sign ($). If you forget it, PHP will not recognize the variable correctly, which can lead to errors or unexpected behavior.
Example
$userName = "Rohan"; $userAge = 22; echo $userName;
Rules for Declaring Variables
When creating variables in PHP, keep these rules in mind:
- A variable must start with a
$symbol - It must begin with a letter or underscore (
_) - It cannot start with a number
- Variable names are case-sensitive (
$nameand$Nameare different) - Avoid spaces and use meaningful names (
$userNameinstead of$x)
Understanding Data Types in PHP
PHP automatically assigns a data type to a variable based on the value you give it. This is known as a loosely typed language, meaning you don’t need to declare the type manually.
Common Data Types
String
Used to store text.
$name = "Rohan";
Integer
Used for whole numbers.
$age = 25;
Float (Double)
Used for decimal numbers.
$price = 99.99;
Boolean
Represents true or false values.
$isLoggedIn = true;
Array
Used to store multiple values in a single variable.
$colors = ["red", "blue", "green"];
Null
Represents a variable with no value.
$data = null;
How to Check a Variable’s Data Type
You can use the var_dump() function to inspect a variable’s type and value. This is especially useful while debugging.
$name = "Rohan"; var_dump($name);
This will output the data type along with the value stored in the variable.
What are Constants in PHP?
Constants are identifiers whose values cannot be changed during the execution of the script. They are useful for storing fixed values like site name, configuration settings, or API keys.
Defining a Constant
define("SITE_NAME", "MasterPHP");
Using a Constant
echo SITE_NAME;
Unlike variables, constants do not use the $ symbol.
Using const Keyword (Modern PHP)
PHP also allows you to define constants using the const keyword.
const PI = 3.14;
This method is commonly used inside classes, but it can also be used in general scripts.
Common Mistakes Beginners Make
1. Forgetting the Dollar Sign
name = "Rohan"; // Incorrect
Without the $, PHP will not treat this as a variable.
2. Invalid Variable Names
$123name = "test"; // Incorrect
Variables cannot start with a number.
3. Confusing Assignment and Comparison
$x = 5; // Assignment $x == 5; // Comparison
Using == instead of = can break your logic if misunderstood.
Real-World Example
define("SITE_NAME", "MasterPHP");
$userName = "Rohan";
$userAge = 22;
$isPremiumUser = true;
echo "Welcome to " . SITE_NAME . ", " . $userName;
This example shows how variables and constants work together in a simple application scenario.
Practice Exercise
Try the following on your local setup:
- Create a variable
$cityand assign your city name - Create a constant
COURSE_NAMEwith value "PHP Basics" - Print both values using
echo
Summary
In this lesson, you learned how PHP handles data using variables, data types, and constants. Variables allow you to store and update values, data types define what kind of data you’re working with, and constants help you store fixed values safely.
Understanding these fundamentals is essential before moving on to more advanced topics.