Variables in PHP
Variables in PHP are used to store information, such as strings, numbers, arrays, and objects. Variables are declared with a dollar sign ($) followed by the variable name and are assigned a value with the assignment operator (=).
For example:
$name = "John";
In this example, the variable $name is declared and assigned the string value “John”.
Variables can also be used to store the results of calculations, such as the sum of two numbers:
$x = 5;
$y = 10;
$sum = $x + $y;
In this example, the variables $x and $y are declared and assigned the integer values 5 and 10, respectively. The variable $sum is then declared and assigned the value of $x + $y, which is 15.
Variables can also be used to store the result of a function call, such as the result of a strlen() function, which returns the length of a string:
$name = "John";
$length = strlen($name);
In this example, the variable $name is declared and assigned the string value “John”, and the variable $length is declared and assigned the value returned by the strlen() function, which is 4.