14 Jul PHP Variables
For storing information in PHP, use variables. Variables are also known as containers.Ā For example, easily store numeric values such as 10, 20, 30, etc in variables. With that, also store any string in variables as shown in the first PHP program, etc. Letās learn about the syntax,
Syntax
Variable in PHP begins with a $ sign. After that, type the variable name. Hereās an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!DOCTYPE html> <html> <body> <?php $a = 10; $b = 20; $result = "We have two values:"; echo $result." ".$a." and ". $b; ?> </body> </html> |
Here’s the output,
As shown above, the following can be seen, with three variables,
- Variable $a holds the value 10
- Variable $b holds the value 20
- Variable $result holds the value; We have two values, as shown above.
- Any text inside quotes is a string.
PHP Variable Rules
Some rules are defined for the usage of variables in PHP. Letās see them one by one with examples:
- A variable name must start with a letter or the underscore character.
For example, $dept - A variable starts with the $ sign, followed by the name of the variable.
For example, $result - A variable name cannot start with a number.
For example, ā2valueā isnāt the correct way to name a variable. - A variable name can only contain alpha-numeric characters and underscores.
For example, result_rank, value1 - Variable names are case-sensitive.
For example, $result and $RESULT are two different variables.
No Comments