You may have learned the previous PHP tutorials about how to write PHP code and how to install local server and run PHP on localhost. So, this tutorial will discover PHP variables understanding and learn how to write PHP variables in coding.
Understanding PHP Variables
PHP variables are only storage area where you can apply and propel them in your code. You can store text, numbers, or the combination of those two types of data into the PHP variables.
Just like in any other programming language which works with variables as the containers for storing temporary data. So, to make it simple for you to understand, a variable in PHP is a temporary data storage in programming. PHP variables can store data in the form of integer, string, and so on. In other words, a PHP variable is a name that refers to any value, while its value represents text.
And you can update or change the value (the content) of your PHP variables. Here we are going to practice how to get started with PHP variables. You can give a name to each one of your PHP variables with an aim to facilitate you in recognizing them. And it would be easier for you to call your PHP variables.
How to Write PHP Variables
There are inescapable provisions in writing PHP variables. So, this PHP tutorial will guide you how to write a correct variable in PHP coding. Here is how to write a correct variable in PHP that complies with the PHP provisions.
- To write a PHP variable, you must start with a $ sign.
- PHP variables are case sensitive to its name’s uppercase and lowercase.
- Then, to fill a variable, you can directly add a = sign after space before the name of the variable.
Those three points are the rules how to write a PHP variable. Take note that how to write a correct PHP syntax as described in the previous PHP tutorial; Introduction to PHP that PHP syntax writing must be started with the PHP opening tag (<?php) and ends with the closing PHP tag (?>). Then, end each line with a semicolon (;).
Next, in the second point explains that PHP variables are case sensitive properties, which means that every variable writing in PHP is very case sensitive. For example, a variable with a $fullName has a different value to a $fullname.
The third point explains that to store a value or information to a variable, you can use an equal sign (=).
Again, when you are getting started with a variable in PHP, you must start with a dollar sign ($), and then be followed with the name of the variable, and to input the value you can directly add an equal sign (=) a space after the name and then followed with the value a space after the “=” sign. Take a look at a sample of PHP code below;
<?php $name = "Jones"; $age = 32; ?>
This example is how to store data or information into a PHP variable. Take note that when you are filling the data in the form of a string, then, the data must be flanked by a couple of double quotation or one quotation marks. But if the value of the variable is numeric, no need use quotation marks.
To display the data on the screen, you must use echo command in PHP. Here we will apply how to display the value of every variable to the screen.
Create a PHP file and save it on your localhost. On this example, I name the PHP file as example.php. Then fill in the following syntax to that PHP file:
<?php $name= "Jones"; $age= 32; echo $name; echo "<br/>"; echo $age; echo "<br/>"; echo "Hi, My name is ".$name." and I'm ".$age." years old.; ?>
Next, access your PHP file using your browser with the address http://localhost/example.php. But make sure you have enabled the Apache.
Pay attention to the syntax above. A dot (.) is used to separate the string and variable. And the <br/> syntax is a break syntax in HTML.
Updating the Names and Values of PHP Variables
You can also update the content of each variable in PHP. Take a look at the following example:
<?php $city = "California"; $country = "USA"; echo $city; echo $country; ?>
You can change the values of the variables and decide whether or not you update the name of each variable. In case you are editing them in a PHP file, make sure you also change every variable which is placed next to every echo of that file. If any of the variables next to an echo doesn’t display data on your screen, it means that the echo-sided variable doesn’t exist in that PHP file.