Variables in PHP
The variables: few provisional data. The advantages of declaring and using variables.
We will start learning to work with the simplest and most universally used data storage: the variables
The variables: few provisional data. The advantages of declaring and using variables
Although to those who are not programmers it still costs a bit of work to imagine the true usefulness of something as simple as a variable, we will continually have the need for the server to remember for a moment some data, either because we have written it ourselves inside of the code that we program, or because the user has entered that data in a form, or because it is the result of an operation performed by the PHP interpreter or that is the result of the execution of a function. All the time we will use variables. And it is not an exaggeration since, practically, we will not realize any page that does not use numerous variables.
For example, if we want to show a greeting that uses the name that a user has just written on a form, we will have to store that data somewhere while we maneuver with it and add a text that goes with it. Or if we want to multiply two numbers (a quantity and a price of a product, for example) we should be able to store somewhere the first number, the second number, and also the result of that operation, and then show it. Or, if we are reusing a page of "Terms and conditions of use" and, between the original site and the new one, it only changes the name of the company, we could have stored the name of the company in a variable, and simply by writing the name new one time in the value of that variable, in all places where we used that variable will automatically appear the name of the new company.
That is to say, the use of the variables will be essential for the most common task of the PHP interpreter, which is to complete part of the texts or the HTML code of a Web page.
For all the tasks that we mentioned -which require "momentarily remembering a data" - there are the variables, which are nothing more than a space in the server's RAM, space that will be temporarily reserved for us to write something inside, and leave it there saved, waiting until we use it instants later, as part of the code that the PHP interpreter is manufactured at that moment.
Each of the spaces that we want to use from the server's memory will be a variable. And each of them will identify with a name that we will invent ourselves, name by which we refer to that data from there, whenever we need to have the content stored in that space.
In a more graphical and simplified way, a variable is a kind of "each label" that stores some data until we need to use it.
We use variables daily; for example, we keep money in a drawer and then we say: "let's use the money in the first drawer". "First drawer would be the name of a variable, which will sometimes contain a value of" 0 "(if there is no money), and sometimes it will contain an" x "amount of money; that is, its content will not always be the same, over time it may vary, hence its "variable" name. Any recipe is full of variables (the amount of each ingredient is a variable).
Even human beings are full of variables! When in a form they say "Write your name here", "your name" is a variable, that box will contain "Hugo", but it will contain any other name in the case that someone is not my namesake. Everything that can be described quantitatively or qualitatively is a data that can be stored in a variable. Weight, height, age, surname, nationality, occupation, telephone, etc. Variables and more variables. Texts and numbers: Data.
A variable always has two elements: a name (always the same, as well as "First drawer" will always be "First drawer", and thanks to that invariability we can refer to it without being confused as "First drawer"), and a value (the data we store inside it) that unlike the name, its may vary.
<?php
$name=”VALUE”;
?>
We can observe, in this example. Some syntax details; in the PHP language, the variables create by putting a $ sign before the name we want to give to the variable (without leaving any space between the sign and the first letter of the name of the variable)
And a value is stored by means of the equal sign, to which we will technically call it "Assignment Operator", since, precisely, it serves to assign a value to a variable.
Let's see other examples to continue understanding this simple syntax:
<?php
$quantity=5;
$price=3;
$amount=$quantity * $price;
printf (“$amount”);
?>
This time we have defined three variables:
- The variable $ amount, to which we have assigned a number 5;
- The variable $ price, in which we will keep a number 3;
- And the variable $ amount, which stores the result of multiplying what contained $ amount so $ price; that is, it will contain a 15 in this case.
Imagine that, at that moment, the server RAM contains this:
Let’s pay attention to the following details:
- Each sentence must end with a semicolon
- Note that values with numerical content - in this case, numbers 3 and 5 - we have not put them in quotation marks; the PHP language distinguishes between several types of data among which the numbers do not need to be wrapped in quotes, unlike text blocks, which do need to be always delimited at the beginning and at the end with quotation marks
- Within the print, we have not put the $ amount variables in quotes either. This is so because if we wrapped it in single quotes, it will consider that it had stored (in this case a 15). However, we would also have the possibility of wrapping it in double quotes and, in this way, the variable itself would normally be replaced by its value
Let's see another example:
<?php
$name=”Peter”;
$message=“Hello, sir, $name”;
print($message);
?>
The variables are a provisional "ephemeral" data repository, which will be used almost immediately.
The work of the PHP interpreter takes place on the server (hosting), and is done in the brief moments between the arrival of an HTTP request executed by a browser and the issuance of a response.
The life of a variable in PHP is very short just a few tenths of a second. At the moment in which the PHP interpreter reads the declaration of a variable, it starts to exist; Then, you can use it for some question (it could be a written order that we have left to the PHP interpreter to use or show that variable, so you can perform an operation with it) and, as far as the PHP interpreter has finished the reading and processing the code of that complete Web page (less than a second, usually) and have delivered the HTML code already processed to the Web server so that it sends it to the browser that was waiting for its response, in that instant, the PHP interpreter will erase of the RAM of the Web server all the variables that would have been declared during the brief time that delayed the processing of that page.
ChatGPT Free
Ask questions on any topic
CITE ARTICLE
For homework, research, thesis, books, magazines, blogs or academic articles
APA Format Reference:
Delgado, Hugo. (2019).
Variables in PHP.
Retrieved Oct 06, 2024, from
https://disenowebakus.net/en/php-variables