$_ has any meaning in question?

Asked

Viewed 143 times

-2

In "execution blocks" I see a lot $_SESSION for validation of forms and related, but did not understand very well what exactly means.

  • Your question is about the global variable $_SESSION or about the prefix $_?

  • Well, in case you two

  • @Problematic Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

3 answers

5

These are predefined variables, as stated in documentation. They are populated by the PHP virtual execution machine according to the execution context, so they are like exposed internal objects for your code to access.

It’s a simple and useful model, but if it were today I doubt that language would use something like this, since language is changing to be more Nterprise and less than script, where the context fit.

Specifically the $_SESSION keeps a unique code indicating which session is active, so a script identify that his call is still part of an execution of the same session of some user and can thus give continuity to what he did in previous executions. The web is stateless, then it doesn’t save the situation between a call and another, its code has to deal with it, and a session identifier is the way to know which is the session, usually it is passed between the browser and the HTTP server by cookies.

Other cookies can be accessed by the variable $_COOKIE. Data from the browser can be picked up by $_GET, $_POST, and $_FILES to catch files being uploaded, or $_SERVER for specific execution environment and call data; or $_ENV for the general environment

$_REQUEST is indifferent between the get and post, and can be used when no matter what form the request came in.

Among some others, even with slightly different standards, still has the $GLOBALS to store values accessible by all script (which is usually very fast).

1

The superglobal variables

These variables are superglobal, and as the name says, their scope is global, meaning they can be accessed from anywhere in the code. Regarding the name starting with $_ has no purpose, it is just a default name for this type of variable.

PHP has the following superglobal variables:

  • $_POST: Contains data from an HTTP post request.
  • $_GET: Contains data from an HTTP get request.
  • $_SESSION: Stores the session data.
  • $_COOKIE: Stores the data of cookies.
  • $_FILES: Stores information about a file uploaded.
  • $_REQUEST: Stores the data of a request, can be post or get.
  • $_SERVER: Contains context and environment information where the script is running.
  • $_ENV: Contains environment variables placed through the function putenv. The values obtained with $_ENV can also be obtained by the function getenv.

-3

Whenever you find variables that start with $_ it means they are super global variables, that is, they are special variables that can be used in any part of your project, and each of them has a specific function.

For example $_SESSION['example'] creates a session, $_GET takes the values of the variables of the URL...etc...

Click here to know more about.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.