Variables store individual values that can be used in PHP expressions. The dollar sign is used to define variables $. For example:
$num;
The variable is defined here as $num. Because a variable definition is a separate statement, it ends with a semicolon.
Typically, variable names begin with a lowercase letter or underscore. It is worth considering that PHP is a case-sensitive language, which means that the variables $numwill $Num represent two different variables.
Also, when naming variables, we need to consider the following rules:
Variable names must start with an alphabetic character or an underscore
Variable names can only contain characters: a-z, A-Z, 0-9, and underscore
Variable names must not include spaces
The assignment operator ( =) assigns a value to a variable:
$num = 10;
Here, a variable is defined $num that stores the number 10.
After defining a variable and assigning a value to it, we can use it in PHP expressions. For example, display its value on a web page:
<!DOCTYPE html> <html> <head> <title>easywptutorials.com</title> <metacharset="utf-8" /> </head> <body> <?php // definition of $num variable $num = 10; // displaying the value of the $num variable on the web page echo $num; ?> </body> </html>
Using a function, the echo value of a variable $numis displayed on a web page. And when accessing the script, we will see the value of the variable $num:
A distinctive feature of variables is that we can change their value:
<?php /// definition of $num variable $num = 10; // displaying the value of the $num variable on the web page echo $num; // change the value of the variable $num = 22; echo $num; ?>
You can also assign a value to another variable:
$a = 15; $b = $a; echo $b;
If a variable is declared, but it is not initially assigned any value (in other words, it is not initialized), then it will be problematic for us to use it. For example:
<?php $num; echo $num; ?>
When trying to display the value of a variable, we will get a diagnostic message that the variable is not defined:
Warning: Undefined variable $num in http://localhost/php_tutorials/hello.php
Therefore, before using a variable, it must be assigned an initial value.
Displaying the value of a variable
In the previous examples, the echo command was used to display the value of a variable , followed by the value to be displayed. However, there is another way to display the value of a variable. For example, we want to display the values of two variables at the same time:
<?php $num_1 = 11; $num_2 = 35; echo "num_1 = $num_1 num_2=$num_2"; ?>
Here echo string is passed to the function. To embed the value of a variable in a string, specify the name of the variable along with the $ sign in this string. If the code in the PHP line encounters the expression $num_1, it will replace this expression with the value of the $num_1 variable. The same goes for the variable $num_2. As a result, when this script is executed, the browser will display the values of both variables:
num_1 = 11 num_2=35