0
0
PHPprogramming~5 mins

Variable naming rules in PHP

Choose your learning style9 modes available
Introduction

We use variable names to store and find information easily in our code. Good names help us and others understand the code better.

When you want to save a number or text to use later in your program.
When you need to remember a user's name or age in a program.
When you want to keep track of a score in a game.
When you want to store a result from a calculation to use again.
When you want to make your code clear and easy to read.
Syntax
PHP
$variable_name;

Variable names in PHP start with a dollar sign $.

Names can include letters, numbers, and underscores, but cannot start with a number.

Examples
A simple variable name to store age.
PHP
$age;
Variable name with an underscore to separate words.
PHP
$user_name;
Variable name with numbers allowed after the first letter.
PHP
$score123;
Variable name can start with an underscore.
PHP
$_temp;
Sample Program

This program shows how to create variables with valid names and print their values.

PHP
<?php
$age = 25;
$user_name = "Anna";
$score123 = 100;
$_temp = 5;

echo "Age: $age\n";
echo "User: $user_name\n";
echo "Score: $score123\n";
echo "Temp: $_temp\n";
?>
OutputSuccess
Important Notes

Variable names are case-sensitive: $Age and $age are different.

Do not use spaces or special characters like !, @, or - in variable names.

Try to use meaningful names so your code is easier to understand.

Summary

Variable names start with $ and can have letters, numbers, and underscores.

They cannot start with a number and are case-sensitive.

Good names make your code clear and easy to read.