The best way to introduce variables is to start thinking about math. You might remember that there are these things called x and y variables that used to store numbers.

If x = 2 and y = 5 then what is x + y? Just substitute 2 for x and 5 for y and you get 7.

Variables are just storage containers. You can store any data type that you would like inside of them; we’ll look at strings and integers below.

In PHP, variables start with a dollar sign, followed by either the underscore or a letter, followed by a series of letters, numbers, and/or underscores. Let’s look at some examples of valid PHP variable names.

<?php
// Valid PHP variable names
$x = 1;
$X = 2; # different from $x
$_x = 2;
$__y = 3;
$_z_ = "Hello";
$__name__ = "Dino";
$dino1 = "First Dino";
$dino_1 = "Dino 1";
$_dino_1 = "He was number 1";
$d1_2_3_hello_ = "Still valid";
// Invalid PHP variable names
$1 = "Number 1";
$%abc = "Invalid"; // Really, any special character
?>

As a regular expression, the valid syntax for PHP variables is stated as:

^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$

#php #programming #computer-science #software-development #web-development

PHP 7.x — P3: Variables Intro
1.10 GEEK