Lesson 6 of 50 – Variables in PHP
12%

Variables in PHP

Variables are used to store data in a program. In PHP, a variable starts with a dollar sign ($) followed by its name. Variables can store values such as text, numbers, decimal numbers, and Boolean values.

Note: PHP is dynamically typed. You do not need to declare a variable's data type before assigning a value to it.
What is a Variable in PHP?

A variable is a named storage location used to hold a value that can be accessed or changed while a program runs.

Example:
<?php
$name = "Rahul";
echo $name;
?>
Output:
Rahul

Here, $name is the variable and "Rahul" is its value. The echo statement displays that value.

Rules for Naming PHP Variables

PHP variable names must follow certain rules.

  • A variable name must begin with the dollar sign ($).
  • The first character after $ must be a letter or an underscore.
  • A variable name cannot begin with a number.
  • Variable names can contain letters, numbers, and underscores.
  • Spaces are not allowed in variable names.
  • Variable names are case-sensitive.
Valid Variable Names:
$name = "Rahul";
$age = 20;
$_city = "Patna";
$student_name = "Amit";
$marks1 = 85;
Invalid Variable Names:
$1name = "Rahul";
$student name = "Amit";
$my-name = "Ravi";

The invalid examples contain a starting number, a space, or a hyphen. Use letters, numbers, and underscores in valid variable names.

Assigning Values to Variables

The assignment operator (=) assigns a value to a variable.

Example:
<?php
$name = "Amit";
$age = 21;
$marks = 89.5;

echo $name;
echo "<br>";
echo $age;
echo "<br>";
echo $marks;
?>
Output:
Amit
21
89.5

PHP determines the type of value assigned to each variable.

Types of Values Stored in Variables

PHP variables can hold values of different data types.

Data Type Example
String $name = "Rahul";
Integer $age = 20;
Float $price = 99.50;
Boolean $is_active = true;
Array $colors = ["Red", "Blue"];
Null $result = null;
Example:
<?php
$name = "Rahul";
$age = 20;
$price = 99.50;
$is_active = true;

var_dump($name);
var_dump($age);
var_dump($price);
var_dump($is_active);
?>
Output:
string(5) "Rahul"
int(20)
float(99.5)
bool(true)

The var_dump() function displays information about a variable, including its data type and value.

Changing Variable Values

You can assign a new value to a variable after it has been created.

Example:
<?php
$name = "Rahul";

echo $name;
echo "<br>";

$name = "Amit";

echo $name;
?>
Output:
Rahul
Amit

The first echo displays the original value. The second assignment changes the value stored in $name.

Case Sensitivity of Variables

PHP variable names are case-sensitive. Variables that differ in capitalization are treated as separate variables.

Example:
<?php
$name = "Rahul";
$Name = "Amit";
$NAME = "Ravi";

echo $name;
echo "<br>";
echo $Name;
echo "<br>";
echo $NAME;
?>
Output:
Rahul
Amit
Ravi

The variables $name, $Name, and $NAME are three different variables.

Using Variables in Calculations

Variables can be used in mathematical calculations and expressions.

Example:
<?php
$a = 10;
$b = 5;

$sum = $a + $b;
$difference = $a - $b;
$product = $a * $b;
$quotient = $a / $b;

echo "Sum: " . $sum;
echo "<br>";
echo "Difference: " . $difference;
echo "<br>";
echo "Product: " . $product;
echo "<br>";
echo "Quotient: " . $quotient;
?>
Output:
Sum: 15
Difference: 5
Product: 50
Quotient: 2

The arithmetic operators perform calculations using the values stored in the variables.

Displaying Variables with echo

You can display variables directly or combine them with text using concatenation.

Example:
<?php
$name = "Rahul";
$course = "PHP";
$duration = 3;

echo "Student Name: " . $name;
echo "<br>";
echo "Course: " . $course;
echo "<br>";
echo "Duration: " . $duration . " months";
?>
Output:
Student Name: Rahul
Course: PHP
Duration: 3 months

The dot (.) operator joins strings and variable values together.

Key Points
  • Variables store values in a PHP program.
  • PHP variable names begin with $.
  • The assignment operator (=) assigns values.
  • PHP variables can store different data types.
  • Variable names cannot start with numbers.
  • Spaces and hyphens are not allowed in ordinary variable names.
  • PHP variable names are case-sensitive.
  • Variable values can be changed after assignment.
  • The var_dump() function displays a variable's type and value.
Practical Assignment

Complete the following tasks:

  1. Create a variable to store your name.
  2. Create a variable to store your age.
  3. Create a variable to store your course name.
  4. Display all three variables using echo.
  5. Create two numeric variables and display their sum.
  6. Change the value of a variable and display its new value.
  7. Create $name and $Name and observe their different values.
Practice Example:
<?php
$name = "Rahul";
$age = 20;
$course = "PHP Programming";

echo "Name: " . $name;
echo "<br>";
echo "Age: " . $age;
echo "<br>";
echo "Course: " . $course;
?>
Output:
Name: Rahul
Age: 20
Course: PHP Programming

🧠 Quick Quiz

1. Which symbol is used at the beginning of a PHP variable name?




2. Which is a valid PHP variable name?




3. Are PHP variable names case-sensitive?