Introduction
Variables are one of the most fundamental concepts in any programming language. They are used to store data that can be used and manipulated throughout a program.
PHP variables are unique compared to many other programming languages because they are loosely (weakly) typed. This means you do not need to declare the data type of a variable before using it. PHP automatically determines the data type based on the value assigned.
What is a PHP Variable?
A PHP Variable is a container used to store data such as strings, numbers, or arrays. The value stored in a variable can be changed during program execution.
- Variables start with a dollar sign ($).
- No need to declare data type explicitly.
- Values can be updated anytime.
Example:
<?php
$name = 'John';
?>
Note: Here, $name is a variable that stores the value ‘John‘.
Syntax
1. Basic Variable Syntax
<?php
$variable_name = value;
?>
2. Example Using echo
<?php
$name = 'John';
echo $name;
?>
Output:
3. Example Using print
<?php
$name = 'John';
print $name;
?>
Output:
Note: Both echo and print are used to display variable values.
Declaring PHP Variables
PHP variables can be declared using:
- Letters
- Underscore (_)
- Numbers (but not at the beginning)
1. Valid Variable Example (Letter)
<?php
$name = 'John';
echo $name;
?>
Output:
2. Valid Variable Example (Underscore)
<?php
$_name = 'John';
echo $_name;
?>
Output:
Invalid Variable Example
<?php
$1_name = 'John';
echo $1_name;
?>
Output:
Note: Variable names cannot start with a number.
Weakly Typed Nature of PHP
PHP is a weakly typed language, meaning:
- You donβt need to define the data type.
- PHP automatically converts the type.
Example:
<?php
$name = "John";
$name = 25;
echo $name;
?>
Output:
Note: The variable $name first stores a string and later stores a number.
Variable Rules
1. Must Start with $
$name = "John"; // Correct
2. Cannot Start with Number
$1name = "John"; // β Invalid
3. Case Sensitive
$name = "John";
$Name = "Doe";
echo $name;
echo $Name;
Output:
Doe
4. Use Meaningful Names
βοΈ Good:
$userName = "John";
β Bad:
$x = "John";
Indirect Reference (Variable Variables)
PHP allows you to use a variableβs value as the name of another variable. This is called indirect reference or variable variables.
Example:
<?php
$car = 'TataNexon';
$TataNexon = "This is developed by Tata Motors";
echo $$car;
?>
Output:
Explanation:
- $car stores ‘TataNexon’
- $$car refers to $TataNexon
Real-Life Example
Example: User Greeting System
<?php
$userName = "John";
echo "Welcome " . $userName;
?>
Output:
This is commonly used in:
- Login systems
- User dashboards
- Profile pages
Common Mistakes
1. Missing $ Symbol
β Wrong:
name = "John";
βοΈ Correct:
$name = "John";
2. Starting Variable with Number
β $1name β Invalid
3. Case Sensitivity Confusion
$name β $Name
4. Forgetting Semicolon
β Wrong:
echo $name
βοΈCorrect:
echo $name;
5. Overusing Variable Variables
Using $$ can make code confusing.
Conclusion
PHP variables are a core concept in web development. They allow developers to store, manage, and manipulate data efficiently. PHP variables are weakly typed.
By understanding variable syntax, naming rules, and concepts like variable variables, you can write cleaner and more effective PHP code. However, it is important to avoid common mistakes such as incorrect naming or missing symbols.
PHP Variable β Interview Questions
Q 1: What is a variable in PHP?
Q 2: How to declare a variable in PHP?
Q 3: Are PHP variables typed?
Q 4: Can variable names start with a number?
Q 5: What is variable scope in PHP?
PHP Variable β Objective Questions (MCQs)
Q1. PHP variables start with which symbol?
Q2. Which variable name is valid in PHP?
Q3. Variable names in PHP are ______.
Q4. PHP variables can store ______ types of values.
Q5. Which is correct way to assign a value to a variable?