How to Install Operating System ??Complete Guide Installing The Operating System

How to Write Variables In PHP

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.


How to Write Variables in PHP

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:

  1. After the “$” dollar character must use the letter or underscore character “_”.
  2. After the character “$” dollars may not use numbers.
  3. In writing variables, you cannot use special characters such as * and ^.
  4. 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.


Then the following is the writing of the rejected PHP Variables.

$ebook computer // cannot use spaces
$| // may not contain special characters
$1bw // after the character "$" dollars may not use numbers

Examples of giving the same but different variables.


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 $description1;
     echo "<br />";
     // example using quotation one
     $version2 = '5';
     $description2 = 'PHP$version2 Tutorial';
     echo $description2;

From the two examples of writing variables by quoting one and quoting two, it will produce the output below:

Writing Variables
Writing Variables

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:

    $a = 1;
    $b = 2;
    $c = $a + $b;
     echo $c; // output 3

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
     // output
     echo $a;
     echo b;

.. output

Writing Variables
Writing Variables

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:

     $a = 'This is the variable $a<br />'; // ordinary variable
     define('b', 'this is variable b'); // constant variable
     // output
     $a = 'variable $a<br />';
     echo $a;
     define('b', 'variable b');
     echo b;
Writing Variables
Writing Variables

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.

whilexor_ _FILE_ __ _LINE_ _
_ _sleep_ _wakeup$_COOKIE$_ENV

Before proceeding, there are a number of articles that might be looking for.


User Review
0 (0 votes)

Add a Comment

Your email address will not be published. Required fields are marked *