07 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 such as Our first program, etc. Let’s learn about the syntax,
Variable in PHP begins with a $ sign. After that, type the variable name. Here’s an example:
$a = 10;
$b = 20;
$result = "We have two values:";
echo $result." ".$a." and ". $b;
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.
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 a 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.