Lesson 4: PHP Variables

الدرس 4 : PHP Variables

Lesson 4: PHP Variables

Introduction

Variables are one of the most fundamental concepts in PHP. They allow developers to store and reuse data throughout the program. Without variables, coding becomes rigid and complex.


What Are Variables?

A variable is a name used to store a specific value, such as a number, a string, or even an array.
In PHP, all variables start with the $ symbol.

Example:

$name = "Ahmed"; 
$age = 25;

Rules for Naming Variables in PHP

When writing variables in PHP, follow these rules:

  • Must start with a $

  • The first character after $ should be a letter or underscore _

  • No spaces or special symbols allowed

  • Variable names are case-sensitive

Correct example:

$_userName = "alaa";

Incorrect example:

$123user = "Mohamed"; // Invalid: starts with a number

Types of Values You Can Store

PHP supports various data types inside variables:

  • Strings: e.g., "Hello"

  • Integers: e.g., 100

  • Floats: e.g., 10.5

  • Booleans: true or false

  • Arrays: to hold multiple values

  • Objects: for object-oriented programming


How to Print Variables

To display a variable’s value, use echo or print:

$name = "Alaa"; 
echo "Welcome, $name!";

Why Variables Matter

Variables provide a dynamic way to manage data. You can change their values at runtime, which adds flexibility and control to your application logic.


Conclusion

Mastering PHP variables is a must for any developer. They help write cleaner, reusable code. The more skilled you become at using them, the better your PHP applications will be.

Important links Portfolio

Share with your friends

العودة إلى المقالات