In PHP, a variable is a named container that can hold a value. The value of a variable can be changed throughout the program.
To create a variable and assign a value to it, you use the following syntax:
“`php
$variable_name = value;
“`
The dollar sign ($) is used to declare a variable in PHP.
Here’s an example:
“`php
$name = “John”;
$age = 25;
“`
In this example, the variables `$name` and `$age` are created and assigned the values `”John”` and `25` respectively.
Variables in PHP are case-sensitive, which means `$name` and `$Name` are considered two different variables.
You can also use variables to store the result of an expression or the return value of a function:
“`php
$total = $price1 + $price2;
$length = strlen($text);
“`
In these examples, the variable `$total` is assigned the sum of `$price1` and `$price2`, while the variable `$length` is assigned the length of the string stored in `$text`.