A PHP Variable is simply an element or data item that may take on more than one value during the run-time of a program.
In other words, a PHP variable store a value that can change, depending on conditions or on information passed to the program.
PHP Variables Rules:
- In PHP, variable names are case-sensitive.
- Variables in PHP are represented by a dollar sign followed by the name of the variable.
- A valid variable name starts with a letter or underscores, followed by any number of letters, numbers, or underscores.
- After declaring a variable it can be reused throughout the code.
- A variable does not need to be declared before adding a value to it.
- PHP automatically converts the variable to the correct data type, depending on its value.
- The assignment operator
(=)used to assign value to a variable.
PHP Variables Example:
<?php $content = "Hello PHP!"; $a = 10; $_msg = 'Hello again'; echo $content; echo "<br>".$a; echo "<br>".$_msg; ?>
|Exercises & Assignments|
|Write a program to print “Hello World” using echo|
|Write a program to print “Hello PHP” using variable|
|Write a program to print a string using echo+variable.|
|Write a program to print two variables in single echo|
|Interview Questions & Answers|
|How to check if a variable is empty in PHP?|
|Is PHP a case sensitive language?|
|What are the different data types in PHP?|