Superglobal variables in PHP





How to use Superglobals Variables in PHP

 

What is Superglobal variables in PHP?

In PHP  superglobals are predefined variables. Superglobal can be used to make variables available in every page without declaring them global. They are project level variables, it means  scope of those variables throughout the entire web application. They can be accessed from any page within the web application. Superglobal variables prefixed with $_ and followed by  some name. The name is written in capital letters. It is a predefined name and cannot be changed.


  • $_SERVER
  • $_FILES
  • $_COOKIE
  • $_SESSION
  • $_GET
  • $_POST
  • $_REQUEST
  • $_ENV
  • $GLOBALS

In this article we are going to use 3 super global variables.

$_GET :  Allows 100 char length URLs. URL can be added to bookmarks.
$_POST:  Max limit 8 kb. can be changed.

  •      Post_max_size defined in php.ini.
  •      URL not possible for bookmark.
  •      URL cant be visible in the browser.

$_REQUEST: can read $_GET, $_POST, $_COOKIE values.

 

1. Using GET variable:

In the previous blog we just worked on a simple form which sends the get request. Here I will use the same example form and connect with a PHP file which will display Get data.

 

  1.  Create one project folder in Server
  2.  Create 2 files: 1.hello.html, 2. hello.php in the project.
  3.  Open hello.html and enter some name
  4.  Click Go button
  5.  PHP file will display the name that you entered in form

 

hello.html

<html>
 
<body>
 
<form name="sampleform" action="hello.php" method="get"> 
 
Enter Name:<input type="text" name="user">
 
<input type="submit" name="submit" value="Go" >
 
</form> 
  
</body>
 
</html>

 

coding-zon-hello-form

hello.php

 


<?php
	
echo "Hello ".$_GET["user"];

?>

 

 

coding-zon-hello-php

This image may explain you better. How Get request works.

When form submitted URL submits the PHP filename and form data to the server. Server will opens the PHP file, PHP file stores all the submitted data in $_GET. Using $_GET we can print form data.

So in the above example when user entered some name and clicked on Go button the URL sending the request as shown in the below


http://localhost/coding-zon/phpforms/hello.php?user=John&submit=Go

when request received, server immediately opens the hello.php. PHP file stores submitted data in a global array called $_GET variable. Next echo statement read the 'user' value from $_GET array as shown, $_GET['user'] and prints it, with greeting message  'Hello' as a response.

 

coding-zon-get-request

 

 

 

Next - Learn How to Create Login form

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP