PHP Variables
Creating PHP Variables
Variables in PHP are just like variables in Javascript. They can be of different types of data saved in the variables - Strings, Booleans, Numbers, Dates, or Arrays. All variable names start with a dollar sign.
Note
There is NO keyword var
in PHP.
<?php
$name = "John";
$age = 33;
?>
The above example creates two variables - one called $name
and one called $age
. If you want to change the contents of a variable then you would write the same thing as you would to create the variable.
<?php
$age = 34;
?>
Now the value inside the variable $age
is 34 instead of 33.
Retrieving PHP Variables
To the value stored inside of a PHP Variable, you simple call the variable name.
<?php $name; ?>
However, this only retrieve the variable value inside of PHP. No content will be displayed in the browser. If we want to write out the value of the variable $name then you can use the echo statement.
<?php echo $name; ?>
Deleting a PHP Variable
If you ever want to delete a variable there is a function called unset()
that will let you destroy a variable.
<?php
unset($age);
?>
The above example is destroying the variable $age
.