I extremely apologise in advance for the wording of the question, if an admin could word it better feel free too. I'm probably going to find this difficult to explain.
I want to create a page which uses a part of the URL to create a "custom" part of the page.
For example www.example.com/hello?=Derek
And the title would read "Hello Derek" or whatever you put after "?=". I know a few websites use this and I was wondering how I would go about doing this.
You're thinking of query parameters, they're of the form key=value
, separated by &
for multiple parameters and by ?
from the normal page URL. So in your case it would be www.example.com/hello?name=Derek
.
As to how to display it in PHP, the following should do it:
<?php
echo 'Hello ' . htmlspecialchars($_GET["name"]);
?>
If www.example.com?hello=Derek
is acceptible to you, you can use the following code in your index.php:
<?php
// Initialize the name variable, so it can be used easily.
$name = '';
// Check if a name was given. If so, assign it to the variable.
// The leading space is there to have a space between 'Hello' and the name.
// If you don't pass a name, the text will just say Hello! without space.
if (isset($_GET['hello'])) {
$name = ' ' . $_GET['hello'];
}
// Personal opinion: Don't echo big chunks of HTML. Instead, close the PHP tag, and
// output the plain HTML using < ?= ... ? > to insert the variables you've initialized
// in the code above.
?>
<h2>Hello<?=$name?>!</h2>