Understanding $_get in PHP - A detailed discussion

This resource is more like notes of someone who has mastered the language →
http://php.net/manual/en/reserved.variables.get.php

But novice like me seems to be struggling even to grasp the basics of $_GET. I know there are lots of Pro’s here. Can some one share their knoledge and help me grasp it in its deepness.

I read few blogs what I understand is when browser send some request an array is created; But could not actually understand anything beyond.

What specifically are you having trouble understanding?

$_GET is not too complex. It is an array of key and value pairs taken from the URL string.

When you see a ? in a URL followed by something=somethingelse these are URL parameters which php may “get” to use in your scripts.

For example if you have:-

https://www.example.com?product=199

…you can get the product paramerter and its value.

if($productId = $_GET['product']){

    $sql = $db->prepare("SELECT * FROM products WHERE id = ?") ;
    $sql->execute([$productId]);
    // Etc...
}
3 Likes

$_GET is a Superglobal array - it always exists, it’s always accessible, and so you can always use it regardless of variable scope and without needing the global keyword. By default it will be empty unless there are some URL parameters, but that doesn’t mean you can’t use it.

PHP slices off the URL string after the ?, and feeds it through the urldecode function to return an array. If there is no ?, then nothing goes through the function, so you get an empty array.

Should you use it? Purists will say you should never write to input superglobals (GET, POST, REQUEST), and should work in local scope instead. Nothing stops you from writing to the $_GET array though; it’s an array like any other.

2 Likes

@codeispoetry,

This is speculation on my part but I’m guessing part of the reason for your confusion is an incomplete understanding of HTTP fundamentals, Especially with how data is transferred between the server and the browser. Here is a fairly straight forward article discussing the basics. It even has napkin sketches.

And one thing that has always helped me out is pressing F12 in your browser and then navigating to the network tab. This allows you to inspect exactly what is being sent back and forth.

2 Likes

Actually the server sends a lot more very useful information than just the $_GET[...] array.

Try this especially on different web pages and spot the differences:

<?php 
echo '<pre>'; // prettified and adds linefeeds
  print_r( $_SERVER );
echo '</pre>';
...
...
// your script

// my favourite usage
defined (‘LOCALHOST’)
||
define(‘LOCALHOST’, ‘localhost’===$_SERVER[‘SERVER_NAME’] );

// usage to ensure unique web-page is valid on both localhost and online servers
if(LOCALHOST):
// don’t bother to load Google Analytics, etc
else:
require ‘google-analytics.php’;
endif;
//

or invert the logic and save yourself an else clause :wink:

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.