Home >Backend Development >PHP Tutorial >PHP variables_PHP tutorial
A variable is a variable amount in a program or used to store numbers, strings or the results of functions.
Once a variable is set, we can use it repeatedly in the script.
All variables in PHP start with the $ symbol.
The correct way to set variables in PHP is:
$var_name = value;
Beginners to PHP often forget the $ sign in front of variables. If you do that, the variable will be invalid.
Next we create a variable that holds a string and a variable that holds a numeric value:
$txt = "Hello World!";
$number = 16;
?>
PHP is a loosely typed language (Loosely Typed Language)
In PHP, there is no need to declare a variable before using it.
In the above example, you see that there is no need to declare the data type of the variable to PHP.
Depending on how the variable is set, PHP will automatically convert the variable to the correct data type.
In strongly typed programming languages, you must declare the type and name of a variable before using it.
In PHP, variables are automatically declared when used.
Naming rules for variables
Variable names must start with a letter or underscore "_".
Variable names can only contain alphanumeric characters and underscores.
Variable names cannot contain spaces.
If the variable name consists of multiple words, they should be separated by underscores (such as $my_string), or start with a capital letter (such as $myString).