"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Saturday, January 20, 2024

PHP $_GET and $_POST

PHP $_GET and $_POST

$_GET and $_POST are superglobal arrays in PHP that are used to collect form data after submitting an HTML form with the method attribute set to "get" or "post" respectively. They allow you to retrieve data from the form fields.

1.$_GET: 

This array is used to collect form data sent in the URL parameters. 

When a form is submitted with the "get" method, the form data is appended to the URL. 

You can access this data using the $_GET array.

Example:

// If the URL is: http://example.com/index.php?name=Lopa &age=100

$name = $_GET['name']; // $name will be "Lopa"

$age = $_GET['age'];   // $age will be 100

It's important to note that these arrays are case-sensitive, so $_GET['name'] and $_GET['Name'] would be considered different variables. 


2. $_POST: 

This array is used to collect form data sent in the HTTP request body. When a form is submitted with the "post" method, the form data is sent in the request body, and you can access this data using the $_POST array.


Example:

// If the form is submitted with fields named "username" and "password"

$username = $_POST['username'];

$password = $_POST['password'];

It's important to note that these arrays are case-sensitive, so $_POST['username'] and $_POST['Username'] would be considered different variables. 

Additionally, both $_GET and $_POST can be used simultaneously in a PHP script depending on how the form is configured.


No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template