Search Example (GET)
What's Happening
Look at the URL! After submitting, you'll see:
get-example.php?q=your%2Bsearch&category=all
This is the query string - data passed via GET is visible in the URL.
The $_GET Superglobal
PHP automatically parses the query string into $_GET:
<?php // URL: ?q=hello&category=books $query = $_GET['q']; // 'hello' $category = $_GET['category']; // 'books' // Always provide defaults for missing parameters $query = $_GET['q'] ?? ''; // Empty string if 'q' not in URL ?>
Current $_GET Contents
Array ( )
Why GET for Searches?
- Bookmarkable: Users can save search results
- Shareable: Copy URL to share with others
- Back button works: Each search is a distinct URL
- Cacheable: Results can be cached by browsers/proxies