Variables are "containers" for storing information:
Rules for PHP variables:
$
sign, followed by the name of the variableA-z
, 0-9
, and _
)$y
and $Y
are two different variables)A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it:
<?php
$name = "Tutorial";
$x = 10;
$y = 20.5;
?>
In the example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on its value.
In other languages such as C, C++, and Java, the programmer must declare the name and type of the variable before using it.
<?php
$var = "Tutorial"; //var become String type
$var = 50; //var become Integer type
$var = 20.5; //var become Float type
?>
Ask Question