Hi Guys,
Today we will learn about PHP Variables.
PHP Variables:
Today we will learn about PHP Variables.
PHP Variables:
Before learning any programming language, you should have knowledge of Variables. So first let's starts with the definition of Variable.
What is Variable?
"Variable is like a container which stores value, such as string value "Hi How are you!" or integer value 1.Instead of typing out the actual value over and over again in your code, assign that value to a variable and after that you can use that variable throughout the code."
How to define variable?
"We use dollar($) sign to define variable in PHP. So in PHP, every variable must starts with dollar($) sign. So if you forgot to put dollar sign in front of variable name, then php parser wont recognize variable."
$variable_name = Value ;
For Example:
$a = "Hi How are you!";
$b = 4;
(Important Note: PHP does not require variables to be declared before being initialized. So you need not to declare variable as string or integer as we defined in C, C++. PHP Parser automatic parse variable as a string or as a integer as an when you initialized the variable.)
For Example:
$a = "Hi How are you!";
$b = 4;
(Important Note: PHP does not require variables to be declared before being initialized. So you need not to declare variable as string or integer as we defined in C, C++. PHP Parser automatic parse variable as a string or as a integer as an when you initialized the variable.)
PHP Code:
| <?php $variable_name = "Hi How are you!"; ?> |
Output:
| Hi How are you! |
Variable Naming Convention:
Every programming language have some standard rules that must be follow when choosing a name for Variable.
Here are some standard rules that need to be follow for choosing name for PHP Variable.
- PHP variables must start with a letter or underscore "_".
- Variables with more than one word should be separated with underscores. e.g $variable_name_surname
- PHP variables are case-sensitive. e.g $variable and $Variable both are different
- PHP variables may only be comprised of alpha-numeric characters and underscores. e.g: a-z, A-Z, 0-9, or _ .
Thanks & Regards,
Rohit
No comments:
Post a Comment