PHP Variables

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.

πŸ“–
Important points:
  • 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:

John

3. Example Using print


<?php
$name = 'John';
print $name;
?>

Output:

John

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:

John

2. Valid Variable Example (Underscore)


<?php
$_name = 'John';
echo $_name;
?>

Output:

John

Invalid Variable Example


<?php
$1_name = 'John';
echo $1_name;
?>

Output:

Syntax error

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:

25

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:

John
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:

This is developed by Tata Motors

Explanation:

  • $car stores ‘TataNexon’
  • $$car refers to $TataNexon

Real-Life Example

Example: User Greeting System


<?php
$userName = "John";
echo "Welcome " . $userName;
?>

Output:

Welcome John

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?
Ans: A variable is used to store data values.
Q 2: How to declare a variable in PHP?
Ans: Variables start with $ followed by the variable name.
Q 3: Are PHP variables typed?
Ans: No, PHP is dynamically typed.
Q 4: Can variable names start with a number?
Ans: No.
Q 5: What is variable scope in PHP?
Ans: Scope defines where a variable can be accessed.

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?






Related PHP Tutorials