Superglobal arrays in PHP
It is an array that stores all the variables that have been sent to the server attached to a link.
Arrays that store data automatically
In the previous chapter, we have seen that the PHP interpreter automatically stores the data available to it in several matrices. One of them is the so-called $ _GET (we pronounce it "script under get").
It is a matrix that stores all the variables that have been sent to the server attached to a link, that is: attached to a browser request made using the get method of the HTTP protocol (technically this is so, but in a more simple, PHP stores, in that matrix $ _GET, the variables added to a link).
As in any matrix, between the brackets of the $ _GET matrix we must indicate the subscript that we want to read at this moment; in this case, it will always be alphanumeric, since the name of the variable that we have sent in the link will be used. In the previous example, we will write "name" inside those brackets, since that was the name of the variable that we sent at the end of the link.
Following the exercise, depending on the link that we click, the content we will see on the destination.php page will be different. If we look at the address bar of our browser, we will notice that the variable sent and its value are visible in the URL of the page.
Some readers will ask: And how do we send more than one variable at a time, in a single link? Simply, joining each pair of variable = value with an ampersand (&) sign. Example of how to send several variables in a single link:
<p>
<a href="data.php?name=Pepe&lastname=Perez&age=17">This is Pepe's link</a><br />
<a href="data.php?name=Pedro&lastname=Garcia&age=9">This is Pedro's link </a><br />
<a href="data.php?name=Juan&lastname=Fernandez&age=30">This is Pedro's link Juan</a><br />
</p>
This code will send three variables to the server: first name and age.
Important note: if this code falls within an XHTML document, like this: & amp;
Therefore, each link will be similar to this:
<a href="datos.php?name=Pedro&lastname=Garcia&age=9">This is Pedro's link</a>
The content of the second file, named datos.php, will look like this:
<?php
print("<p> The values were: ");
print("<br />");
print($_GET["name"]);
print("<br />");
print($_GET["lastname"]);
print("<br />");
print($_GET["age"]);
print("</p>");
?>
Notice in passing that we have instructed the PHP interpreter to write, between data and another, a line break or break (<br />), so that the values are clearly one in each line, and not all then in the same row. A variant of this sending of variables, using the get method, is to directly pass your step directly from the browser, writing their names and their values in the address bar of our browser; This is very practical to test what values are coming to the next page quickly, without having to create a page with a specific link to test it. We will often use this technique to detect errors while programming, if we suspect that the data we expect does not reach the server.
Writing that inside the browser's address bar is equivalent to placing the href attribute with this value inside a link:
http://localhost/recibe.php?name=Pepe&lastname=Perez&age=17
When we press Enter this will be sent to the server and an identical effect will be achieved to the previous links, since clicking a link or writing something in the address bar of a browser achieves the same effect: in both cases, the browser makes a request HTTP protocol, which uses the get method to attach the variables.
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).
Superglobal arrays in PHP.
Retrieved Dec 10, 2024, from
https://disenowebakus.net/en/superglobals-variables-php