Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

Variables

Creating (Declaring) PHP Variables

In PHP, a variable begins with the $ sign, followed by the variable name.

Example

$x = 5;
$y = "John"

In the example above, the variable $x will store the value 5, while the variable $y will hold the value “John”.

Note: When assigning a text value to a variable, enclose the value in quotes.

Note: Unlike many other programming languages, PHP does not require a specific command to declare a variable; it is created as soon as you assign a value to it.

Consider variables as containers for holding data.

PHP Variables

A variable can have a short name (like $x and $y) or a more descriptive name (like $age, $carname, or $total_volume).

Here are the rules for PHP variables:

  1. A variable begins with the $ sign, followed by the variable name.
  2. A variable name must start with a letter or an underscore character.
  3. A variable name cannot begin with a number.
  4. A variable name may only contain alphanumeric characters and underscores (A-Z, 0-9, and _).
  5. Variable names are case-sensitive (e.g., $age and $AGE are treated as distinct variables).
Keep in mind that PHP variable names are case-sensitive!