It’s awkward in php that the variable outside a function aren’t available in the function inside.It’s wired.If you want to access it,here is the way

$variable = "Hello outside?";
function functionCall() {
global $variable;
echo "$variable";
}
functionCall();//Output:Hello outside?

You use the global keyword to tell the PHP that you want to use that variable

Superglobals

The Superglobals are available between inside and outside.They’re associative arrays.

  1. $GLOBALS:Instead of declare the variables like this global $variable,you can use $GLOBALS[“variable”].
  2. $_SERVER: This includes information about the server you are running your PHP and any imfomation about the execution environment.
  3. $_GET: This variable holds any HTTP GET request variables.
  4. $_POST: This one holds any HTTP POST request variables.
  5. $_FILES: This is where any HTTP file uploads are stored.
  6. $_REQUEST: This is your info about the HTTP Request that requested this file.
  7. $_SESSION: This is the array where you store variable that you’ll need across multiple pages
  8. $_ENV: This is where keep the environment information
  9. $_COOKIE:HTTP cookie information.

$_GET

An HTTP GET request is what is sent from your browser to the server when you type a URL or click a link.The url like https://example.com/?q=someexample.The importart part is after the question mark.that part of the questiopn mark is called the querystring and holds the data which is in key-value form.You can write you own querystring.

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
if(isset($_GET["name"])):?>
<p>Hello,<?php echo $_GET["name"]?></p>
<?php else: ?>
<form action="./test1.php" method="GET">
<p><input type="text" name="name"></p>
<button type="submit">Submit</button>
</form>
<?php endif; ?>

The action attribute tell the form what url to send the requests to.The method attribute tells the HTTP method used in the request.We use GET here,it put the data into querystring and append it to the end of the url.GET used to take some non-private data from the user.

$_POST

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
if(isset($_POST["name"])):?>
<p>Hello,<?php echo $_POST["name"]?></p>
<?php else: ?>
<form action="./test1.php" method="POST">
<p><input type="text" name="name"></p>
<button type="submit">Submit</button>
</form>
<?php endif; ?>

POST requests are better for sending secure data because the data is only stored within the headers,we can’t see it.This is why a well written PHP script will use isset to see if needs are set.If not,they provide a senisible fallback.