PHP GET and POST
PHP uses $_GET
and $_POST
to collect form data submitted through HTML forms. $_GET
appends data to the URL while $_POST
sends data in the HTTP request body.
PHP $_GET
और $_POST
का उपयोग HTML forms के माध्यम से भेजे गए डेटा को प्राप्त करने के लिए किया जाता है। $_GET
डेटा को URL में जोड़ता है जबकि $_POST
डेटा को request body में भेजता है।
Live Demo: GET Method
Live Demo: POST Method
Code Example: $_GET
<form method="get" action="">
Name: <input type="text" name="username">
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") {
echo $_GET['username'];
}
?>
(Output will show entered username from URL query string)
Code Example: $_POST
<form method="post" action="">
Name: <input type="text" name="username">
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo $_POST['username'];
}
?>
(Output will show entered username after form submit)
Comparison Table: GET vs POST
Aspect | GET | POST |
---|---|---|
Visibility | Visible in URL | Hidden in request body |
Security | Less secure | More secure |
Usage | Search, filter | Login, forms |
Data Length | Limited | Unlimited |
Bookmarkable | Yes | No |