Search code examples
phphtmlscriptinghref

What is the '?' in a href when calling a php script?


I've seen a ? followed by an id when calling a php in an href. I've been researching, but can't seem to find a straight answer (probably using the wrong terminology)

<a href='page2.php?id=2>

What is the '?' followed by the id here? Any explanation would be appreciated, I'm new to php.


Solution

  • This is a way to pass data within a url from page to page. When the page loads, PHP can access that data with a call like this.

    $id = $_GET['id'];
    

    Then PHP can use this data for its own purposes.

    You can string together data as well like...

    ?id=2&name=james
    

    And again access the data with a call like...

    $name = $_GET['name'];
    

    This is how forms work except when you POST a form, the values are hidden in the url and you retrieve the data like...

    $name = $_POST['name'];
    

    But if your PHP doesn't know if the data will be a POST or a GET, you can use

    $name = $_REQUEST['name'];
    

    Which can grab the data from either a POST or GET.