SoFunction
Updated on 2025-04-09

PHP Tutorial Variable Definition

Variables in PHP
Variables are used to store values ​​such as numbers, text strings, or arrays.
Once a variable is set, we can use it repeatedly in the script.
All variables in PHP start with the $ symbol, and variable names are case sensitive.
The correct way to set variables in PHP is:
$var_name = value;PHP beginners tend to forget the $ symbol before the variable. If you do that, the variable will be invalid.
Although variables are not required in PHP, this is a good habit. Uninitialized variables have default values ​​for their type - FALSE, zero, empty string, or empty array.
Copy the codeThe code is as follows:

<?php
$var = 'PHP';
$Var = 'Tutorial Network';
echo "$var, $Var"; // Output "PHP, Tutorial Network"
$4site = 'not yet'; // Illegal change of name; start with a number
$_4site = 'not yet'; // Legal variable name; start with underscore
$i site is = 'mansikka'; // Legal variable name; can be used in Chinese
?>

Naming rules for variables
The variable name must begin 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, it should be separated by underscores (such as $my_string), or start with capital letters (such as $myString).