PHP Variables
This tutorial will teach you the basics of variables, how they make everything easier for you, and why you should use them!
I will start off by saying variables in PHP are extremely easy, and they make coding a lot easier. I will show you a simple exmaple below.
$name = "Joey";
echo $name;
?>
The above code echoes out the variable $name, which has the value of Joey in it. The above code will output "Joey". Now that I think of it, I should have used "Hello World"... Oh well, too late.. Now, moving on. Variables can be used in many different ways, some of which I will show you in this tutorial.
We will now create a simple sentence with variables with the code below.
$name = "Joey";
$gender = "male";
$age = "16";
$location = "Florida";
echo "Hi there, $name, you are a $gender, you are $age years old, and you live in $location.";
?>
The output of the above code will be: Hi there, Joey, you are 16 years old, and you live in Florida.
After a variable is set in your code, it can be accessed or used as many times as you want.
When naming variables, keep in mind the rules below.
- A variable name must start with a letter or an underscore.
- A variable name can only contain a-Z, 0-9, and a _ (underscore).
- When naming variables do not use spaces, if the variable name is two words, use an underscore _, my_variable. Or capitalize the second word, myVariable.