Define Variables in PHP
In this article, we will discuss how to define variables in php. In PHP variables are something different to define compared to other languages.
Rules for PHP variables
- The best way to store data in PHP is to use variables. We should start the variable with the $ dollar sign.
- A variable name can consist of numbers, letters, and underscores, and no other special character is allowed. The second character must be a letter or underscore.
- If the underscore is the second character, then an alphabet should occur as the third character.
- No size limit to create PHP variables.
- No keyword should be used as a variable.
- PHP variables are case-sensitive.
- Variables in PHP do not have intrinsic types – a variable does not know in advance whether it will be used to store a number or a string of characters.
Valid Example:
$_Strname
$Strname
$strname
$Str21
Invalid Example:
$_1pay // underscore must be followed by a letter
$1strname // Must begin with a letter or underscore
$_var-temp //contains non alphanumeric character(-)