Define variables in PHP is useful to us in all the code we make, since variables are the main tool to manage information and data of our web application to be made.
To define variables in PHP it is not necessary to assign them a type, so we will only have to define the variable name.
The variable names in PHP begin with the $ sign and then the variable name. The variable names in PHP are case sensitive and must begin with a letter or underscore, the rest of the variable can be a letter, number or underscore.
In this way we can have the following variables:
$myvariable;
$_myvariable;
$myvariable1;
Thus the following variables in PHP:
$1variable;
$?myvariable;
To assign a value to a variable in PHP we will use an equal sign followed by the value to be assigned to the variable and a semicolon at the end. Let’s look at some examples:
$variable1="hello world"; //long strings
$variable2='hello world'; //short strings
$variable3="6646564" //number in strings
$variable4=34645; //simple integer
$variable5=57356.5645; //simple decimal number
$variable6=null; //null
The type of the variable depends on the value we assign to it. Thus, if we assign a string, the variable will be of type string and if we assign a number it will be a numeric variable.
If we want to show the value of a variable in PHP we only have to use the echo statement followed by the variable name.
echo $variable1;
It is also good to know that we can concatenate variables or content using the period as a separator.
echo "The sum of ".$num1."+".$num2." is ".$num1+$num2;
I hope that with this simple article you have been able to see how to define and manage variables in PHP.
