Variables in PHP
In this tutorial, we will learn Variables in PHP. Variables are used to store data like to store string or number for example min_age=18. Value of variable can be changed several times throughout the code. It is so easy to define a variable in PHP just write $sign and then a variable name like this $min_age=18. You do not even need to mention the data type of variable. Because PHP will automatically choose the correct data type of variable according to the value stored in a variable.
Basic Things to keep in mind while creating Variables in PHP
- All variables in PHP start with a $ sign, followed by the name of the variable.
- A variable name must start with a letter or the underscore character _.
- The variable name cannot start with a number.
- Variable name in PHP can only contain alpha-numeric characters and underscores (A-z, 0-9, and _).
- The variable name cannot contain spaces.
There are some rules which you should follow while defining and variable name. To initialize a variable you just need assignment operator (=) then write value of variable for example $min_age=18 or $name=”Mohit”.
Now we will check this basic example to understand Variables in PHP.
<?php $age=22; $name="Mohit"; $city="Delhi"; $height=5.9; echo "Hello ".$name." Your age is ".$age." You height is ".$height.", and You are from ".$city; ?>
Note! Please note I am using dot (.) symbol in the above example in echo to concatenate strings.
Spread the love:
Please share this page on social media with your friends..