What are the PHP superglobal variables
Super global variables (Theme: PHP examples)
Superglobals are variables that can be accessed from anywhere in the script at any time. They are therefore in the global scope and the use of the keyword “global” or access via $ GLOBALS is not necessary. Probably the most important superglobal variables are $ _GET and $ _POST, which contain data sent by the user. In detail, there are the following noteworthy superglobals (some more still exist, but they are of little importance or are already out of date):
- $ _GET: All request parameters from the URL. For example, at the URL http://www.example.com/page.php?foo=bar&test=123 the GET parameters are “foo” (with the value “bar”) and “test” (with the value “123”)
- $ _POST: All parameters sent via HTTP POST. Usually these are sent by the user as soon as he submits a completed form with the attribute “method” set to “post”.
- $ GLOBALS: All global variables defined in the script are placed in this array.
- $ _SERVER: All data generated by the web server, such as the URL accessed, the user's IP or file paths.
- $ _COOKIE: Cookies sent by the visitor.
- $ _SESSION: The data of the visitor's session. A session is used to save data across multiple page views.
- $ _FILES: files sent by the visitor.
2nd example: $ _GET
Assuming that a file test.php exists and this is called with test.php? Foo = bar & test = 123 & example:
3. Example: $ _POST
The following example shows a short form that sends its data via POST. After submitting the form, the content of the input field is displayed.
Output before sending the form:
Output after submitting the form (provided the input field remains unchanged on "bar"):
4. Example: $ GLOBALS
In this example, a variable is written to the $ GLOBALS array and called up by the echoMyGlobal () function, which would normally not have access to this variable if it were not in the said array.
5. Example: $ _SERVER
In this example, the superglobal variable $ _SERVER is used to determine information about the IP address of the caller, the call method and the current PHP file.
6. Example: $ _COOKIE
A cookie is set using setcookie (), which in turn is read using $ _COOKIE (a super global). The cookie is set the first time the page is accessed and can therefore not yet be read from $ _COOKIE. It is available the second time it is accessed (provided that the visitor has activated cookies).
Output when the page is viewed for the first time:
Output when the page is viewed the second time:
7. Example: $ _SESSION
First, a $ _SESSION is started using the session_start () function. This is absolutely necessary, otherwise the information in $ _SESSION will be lost if a session has not yet been started or will not be reloaded if a session has recently been started for the visitor. After starting / loading the session, a check is made to see whether the "time" value has already been set. If not, it will be set to the current time. Otherwise it will be issued. When the page is called up for the first time, you only get the message that the time has been set. The time is only displayed from the second call, which remains constant for all subsequent calls. (If you simply reload the page, you must have activated cookies.)
Output on first call:
Output on the second call:
- How exactly do you write in Korean?
- Is micromanagement productive
- Which tourist destination is worth the noise
- Which websites thoroughly discuss marketing strategies for restaurants
- Why are the Indri endangered
- What are the ratings of the American gods
- What is Spielberg's most popular film
- What is best for foreskin dryness
- What is stopping you from writing?
- Can Toronto become the next Bay Area?
- Who ruled Rome during the Roman Republic?
- What do people think of an ENT
- What's so great about Rakesh Jhunjunwala
- What is the best aerobic swim workout
- What is garbage
- Pick your nose disgusting
- How can we make our minds unconditional
- Why does my husband always feel judged
- Which superhero can defeat everyone else
- Can Kenacomb work against acne
- Ignorance can kill you
- How does the monetary valuation work
- What is Celine Dion best known for
- A silicone roof coating is worthwhile