PHP Variables & Constants: Control Data Like a Real Backend Developer
๐ฑ Introduction
PHP Variables and Constants are the backbone of every PHP program. If PHP is the engine of a website, variables and constants are the fuel that keeps it running. Every login system, calculation, and database operation depends on how well you understand this topic.
This chapter explains how PHP stores data, how values change during execution, and when values should never change. Once this becomes clear, writing PHP logic feels natural instead of confusing.
๐ PHP Variables and Constants
๐ฆ What is a PHP Variable?
Explanation in simple English
A PHP variable is a container that stores data which can change during program execution.
Why this concept exists
Programs need memory to store user input, calculations, and results.
Where and how it is used
Variables store names, numbers, form data, database values, and more.
Key exam points
- Variables store data
- Variable values can change
- Variable names start with
$
โ๏ธ Declaring PHP Variables
Explanation in simple English
PHP variables are created when a value is assigned.
Why this concept exists
PHP uses dynamic typing, so no datatype declaration is needed.
Syntax Example
<?php
$name = "PHP"; // string variable
$age = 20; // integer variable
?>
// $name stores text
// $age stores a number
Key exam points
$symbol is mandatory- Variable names are case-sensitive
- No datatype keyword needed
๐ค Rules for PHP Variable Names
Explanation in simple English
Variable names must follow specific rules.
Why this concept exists
Rules avoid confusion and errors.
Where and how it is used
Used while naming variables in programs.
Key exam points
- Must start with
$ - Cannot start with number
- No spaces allowed
- Case-sensitive
๐ PHP Variables Are Case-Sensitive
Explanation in simple English
PHP treats $name and $Name as different variables.
Why this concept exists
It allows flexibility but requires care.
Example
<?php
$city = "Delhi";
echo $city; // correct
?>
Key exam points
- Variable names are case-sensitive
๐ง What are PHP Constants?
Explanation in simple English
A constant is a value that cannot change once defined.
Why this concept exists
Some values should stay fixed, like configuration data.
Where and how it is used
Used for database names, API keys, and fixed settings.
Key exam points
- Constants do not change
- Constants do not use
$ - Constants are global by default
๐ Defining PHP Constants
Using define() Function
<?php
define("SITE_NAME", "My Website");
echo SITE_NAME;
?>
// SITE_NAME is a constant
// Value cannot be changed
Why define() exists
It allows creation of fixed values safely.
Key exam points
- define() creates constants
- Constant names usually written in uppercase
๐ Constant vs Variable
Simple Explanation
Variables change, constants do not.
Example
<?php
$price = 100; // variable
define("TAX", 18); // constant
?>
Key exam points
- Variables use
$ - Constants do not use
$ - Constants cannot be reassigned
๐งฉ PHP Constant Scope
Explanation in simple English
Constants are accessible everywhere in a script.
Why this concept exists
Configuration values should be available globally.
Where and how it is used
Used in large applications and configuration files.
Key exam points
- Constants are global
- No scope limitation
๐ Real-Life or Practical Use
In real projects, variables store user input, form data, session values, and database results. Constants store fixed values like database credentials, site name, and API URLs. This separation keeps applications safe, organized, and easy to maintain.
๐ Exam-Focused Summary
- PHP variables store changing data
- Variable names start with
$ - PHP is case-sensitive
- Constants store fixed values
- define() creates constants
- Constants do not use
$
๐ฏ Conclusion
PHP Variables and Constants give you control over data flow in PHP programs. Strong understanding here makes future topics like conditions, loops, forms, and database operations much easier.
This topic directly connects to backend logic, configuration management, and secure coding practices. Master this once, and every PHP project becomes more structured and reliable.
