Declaring Variables in PHP
Learn how PHP variables work with the $ prefix and dynamic typing.
What Is a Variable?
A variable is a named container that holds a value your program can read and change.
- In PHP every variable name starts with a dollar sign:
$name - Variable names are case-sensitive:
$ageand$Ageare different - Names must start with a letter or underscore, not a digit
PHP is a dynamically typed language — you never declare the type; PHP figures it out from the value you assign.
Assigning Values
Use the = operator to assign a value to a variable:
<?php
$name = 'Alice';
$age = 30;
$pi = 3.14159;
$active = true;
echo $name; // Alice
echo $age; // 30All lessons in this course
- Declaring Variables in PHP
- PHP Data Types Overview
- Echo and Print Output
- Type Juggling and Type Casting