0
0
PHPprogramming~5 mins

Why variables are needed in PHP

Choose your learning style9 modes available
Introduction

Variables store information so you can use and change it later in your PHP program.

When you want to save a user's name to show it again.
When you need to keep track of a number that changes, like a score.
When you want to store input from a form to use it later.
When you want to reuse a value multiple times without typing it again.
When you want to do math with numbers that can change.
Syntax
PHP
$variable_name = value;

Variables in PHP start with a dollar sign $.

You can store text, numbers, or other data in variables.

Examples
This stores the text "Alice" in the variable $name.
PHP
$name = "Alice";
This stores the number 25 in the variable $age.
PHP
$age = 25;
This sets the variable $score to zero, useful for counting.
PHP
$score = 0;
Sample Program

This program saves the name "Bob" in a variable and then shows a greeting using that name.

PHP
<?php
$name = "Bob";
echo "Hello, $name!";
?>
OutputSuccess
Important Notes

Variable names should start with a letter or underscore, not a number.

Variables help keep your code clean and easy to change.

Summary

Variables hold data you want to use or change later.

They start with $ in PHP.

Using variables makes your code flexible and easier to read.