In PHP, variables are declared using the dollar sign ($) followed by the variable name. The syntax for declaring a variable in PHP is as follows:
$variable_name = value;
Here, variable_name
is the name of the variable, and value
is the initial value assigned to the variable. For example, to declare a variable name
with the value “John”, you would write:
$name = "John"
You can also use variables in PHP to store the results of expressions or function calls. For example, the following code declares two variables, a
and b
, and assigns them the values of 2 and 3, respectively. It then assigns the result of adding a
and b
to a third variable, c
.
$a = 2;
$b = 3;
$c = $a + $b;
// c is now equal to 5
Once a variable has been declared, you can access its value at any point in your code by referring to its name preceded by the dollar sign.