PHP Variables are used to store any information. This information can be anything like a person’s mobile number or their address. Variables are the smallest and powerful element of any programming language.
Whenever you create a variable, PHP creates a space in that memory according to that variable in the name of that variable and stores the value of that variable in it. You can access this value from computer memory by the name of variable.
When the program exit occurs after performing operations on the values of variables, the memory of the variables automatically releases. Whether you want to store values of variables permanently in files too.
Creating php variables
Creating variables in PHP is different from any other language. For this you should keep in mind the following things.
1. The name of the variable should be $ sign before.
2. The variable name should start with letter or underscore.
3. Numbers can also be used in the names of variables, but the name of the variable should not be started with the number.
4. There is no need to define the data type before the name of the variable. PHP automatically detects it. When you assign values to variables, the type of value that is variable becomes also the same type.
5. The PHP case is sensitive. That is, num and Num have two different variables.
6. The names of all the variables must be unique.
Generating variables in PHP is being given the general syntax below.
[dollarSign($)] [variableName] = value;
$studentName = "Ram";
$rollNumber = 30;
Initialize variables by default value in PHP. This means that if you assign a variable to any other variable then the value of that variable gets copy to the variable being assigned. If you change the value of any one variable after assigning value to it then it has no effect on the value of the second variable.
Variables in PHP can also be assign by the value reference. When the value is assign by reference, it changes automatically in another variable when the value changes in a variable.
This is because when the value is assign by reference, its location is assign instead of the value, but the variable is stored on it. Thus both variables point to the same value. This is similar to accessing the same value in different ways with different names.
To display the value of PHP variables, you have to use PHP’s predefined functions. By using echo or print function, you can display the variable’s value. In order to display the values of variables, you also have to put $ sign before the name of the variable.
$studentName = "Ram";
echo $studentName."is a good boy.";