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.
A variable is a named storage location used to hold a value that can be accessed or changed while a program runs.
<?php
$name = "Rahul";
echo $name;
?>
Here, $name is the variable and "Rahul" is its value. The echo statement displays that value.
PHP variable names must follow certain rules.
$).$ must be a letter or an underscore.$name = "Rahul";
$age = 20;
$_city = "Patna";
$student_name = "Amit";
$marks1 = 85;
$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.
The assignment operator (=) assigns a value to a variable.
<?php
$name = "Amit";
$age = 21;
$marks = 89.5;
echo $name;
echo "<br>";
echo $age;
echo "<br>";
echo $marks;
?>
PHP determines the type of value assigned to each variable.
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; |
<?php
$name = "Rahul";
$age = 20;
$price = 99.50;
$is_active = true;
var_dump($name);
var_dump($age);
var_dump($price);
var_dump($is_active);
?>
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.
You can assign a new value to a variable after it has been created.
<?php
$name = "Rahul";
echo $name;
echo "<br>";
$name = "Amit";
echo $name;
?>
The first echo displays the original value. The second assignment changes the value stored in $name.
PHP variable names are case-sensitive. Variables that differ in capitalization are treated as separate variables.
<?php
$name = "Rahul";
$Name = "Amit";
$NAME = "Ravi";
echo $name;
echo "<br>";
echo $Name;
echo "<br>";
echo $NAME;
?>
The variables $name, $Name, and $NAME are three different variables.
Variables can be used in mathematical calculations and expressions.
<?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;
?>
The arithmetic operators perform calculations using the values stored in the variables.
You can display variables directly or combine them with text using concatenation.
<?php
$name = "Rahul";
$course = "PHP";
$duration = 3;
echo "Student Name: " . $name;
echo "<br>";
echo "Course: " . $course;
echo "<br>";
echo "Duration: " . $duration . " months";
?>
The dot (.) operator joins strings and variable values together.
$.=) assigns values.var_dump() function displays a variable's type and value.Complete the following tasks:
echo.$name and $Name and observe their different values.<?php
$name = "Rahul";
$age = 20;
$course = "PHP Programming";
echo "Name: " . $name;
echo "<br>";
echo "Age: " . $age;
echo "<br>";
echo "Course: " . $course;
?>
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?