In this basic PHP tutorial, we will discuss Variables in PHP, but we previously told what variables and how to write the correct variables. Variables can be said as containers that are used to allocate storage of values and information that can be increased or variable. Actually in all variable programming languages, the function is the same, but usually, the writing method is different, here we will discuss how to write PHP Variables, for more details, let’s practice using them.
Before going forward, there are a number of articles that you might be looking for.
To be able to declare PHP Variables programming language then it starts with the dollar character “$”, this must be done as a rule requirement in PHP programming. In addition to these basic requirements there are several conditions that must be followed in writing variables, namely:
After the “$” dollar character must use the letter or underscore character “_”.
After the character “$” dollars may not use numbers.
In writing variables, you cannot use special characters such as * and ^.
Writing PHP Variables are case-sensitive.
These rules must be followed so that the program code that is written will not experience problems and writing PHP Variables are case-sensitive meaning that if the naming of variables is the same but the use of different uppercase and lowercase letters will be read as different variables and have their respective values respectively, here is how to write the correct variable.
This difference occurs due to the use of uppercase and lowercase letters which are very influential in writing variables in PHP code, this has become an absolute rule.
How to Save Values in Variables
PHP is like any other programming language, how to give a value to a variable by using the character “=” and ending with a semicolon character (semicolon), for example:
$xzy = 'This text is data from the xzy variable'
The example above is giving a simple string value using a single quote or single quote, then what if you use quotation two or double quote? the fundamental difference between quoting one and two quotations in PHP is the server-side processing, meaning that if a variable uses the quotation one that will be processed by the server is a pure string, conversely if you use two quotes or double quotes then the server will do a two-step process, ie if there is a variable in the output process using quotation two then the server will read it as a variable, different from quoting one it will still be read as a string, let’s look at an example of the difference:
// example uses quotation two
$version1 = "5";
$description1 = "PHP $version1 Tutorial";
echo "<br />";
// example using quotation one
$version2 = '5';
$description2 = 'PHP$version2 Tutorial';
From the two examples of writing variables by quoting one and quoting two, it will produce the output below:
Pay attention to the output that appears in the browser, as said before that if you use one quotation and insert a variable in it, it will still be read as a string, conversely, if you use quotation two, the variable inside it will still be processed as a variable.
Then how can variables store integer values to be able to do calculations? PHP will read an integer value when not enclosed in quotation marks, for example:
If a variable contains a number value, PHP will process it automatically as a variable with a numeric data type.
Writing Constant PHP Variables
Actually, ordinary variables with fleet constants are not much different, both are used to store a value in it, but the basic thing is writing, if the ordinary variable starts with the character “$” dollar then the constant is not necessary but needs to be defined first.
$a = 'This is the variable $a<br />'; // ordinary variable
define('b', 'this is variable b'); // constant variable
In addition, the value of a constant variable is fixed or cannot change if it is re-declared, different from the usual variable which can still be changed if there is the same variable below it with different values, and if forced to redefine a constant variable an error will occur, for example:
From the constant tutorial, it can be concluded that the writing of constant variables can only be done once and one value.
Which Can Not Be Made Naming Variables
There are a number of things that need to be known that in writing variables must pay attention to the naming given, it could be a default variable or keywords which are already available in PHP so that they should not be used for naming the variables themselves. Keywords in PHP can be variables, classes, constants, and functions, which is why we must pay attention to naming variables, classes, functions, and constants, perhaps the variable names used are PHP keywords, here is a list of default keywords in PHP.
_ _FILE_ _
_ _LINE_ _
Before proceeding, there are a number of articles that might be looking for.