Google News
logo
PHP Program to $_GET - Collect data sent in the URL
In the following example of PHP program that demonstrates how to use the `$_GET` superglobal variable to collect data sent in the URL :
Program :
<!DOCTYPE html>
<html>
<head>
	<title>Using the $_GET Superglobal</title>
</head>
<body>

	<?php
	// Check if the 'name' parameter is set in the URL
	if(isset($_GET['name'])){
		// If it is, store the value in a variable
		$name = $_GET['name'];

		// Output a message with the value of the 'name' parameter
		echo "<p>Hello, $name!</p>";
	} else {
		// If the 'name' parameter is not set, output a default message
		echo "<p>Welcome to my website!</p>";
	}
	?>

	<p>To personalize the message, add your name to the URL like this:</p>
	<p>http://localhost/mysite?name=John</p>
	<p>Replace 'John' with your own name.</p>

</body>
</html>
In this example, we first check if the 'name' parameter is set in the URL using the `isset()` function. If it is, we retrieve the value of the parameter using the `$_GET` superglobal and store it in a variable called `$name`. We then output a personalized message using the value of the `$name` variable.

If the 'name' parameter is not set in the URL, we output a default message.

The bottom part of the code provides instructions on how to add the 'name' parameter to the URL to personalize the message.