0

This works very well for me:

<html>
<head>
<title>Query string </title>
</head>
<body>

<?php

// The value of the variable name is found
echo "<h1>Hello " . $_GET["name"] . "</h1>";
 
// The value of the variable age is found
echo "<h1>You are " . $_GET["age"] . " years old </h1>";

?>

</body>
</html>

And the PHP is like this:

$_GET["name"]
$_GET["age"]

My question is, as you can see there is HTML markup in my example, just a simple H1 Tag. But - what happens if the GET parameters are not there? Then the result would be an empty H1 Tag which is not the end of the world, but I'd prefer to only show the HTML Markup IF the GET parameters are present.

Would this be possible and how would I go about it?

I guess this is the case:

if (condition)
   execute statement(s) if condition is true;
else
  execute  statement(s) if condition is false;

So, if that's the case then IF the GET DATA is there then run the HTML?

Henry
  • 5,195
  • 7
  • 21
  • 34
  • Yes it's possible. Use an if statement so it doesn't print the relevant HTML if the variables aren't present. Hint: the isset() function can also help you here. Pretty sure you can research / find examples of this already with a bit of sensible googling – ADyson Aug 18 '20 at 07:56
  • P.s. "the result would be an empty H1 Tag", no it wouldn't, you'd still have the fixed text e.g. Hello , so it would probably look at bit strange. And depending on your settings you might also see a big ugly warning about missing variables. It's trivial to test it yourself and see the results – ADyson Aug 18 '20 at 07:58
  • 1
    E.g. re the googling, your question is basically identical to this (and several others): [How to verify if $\_GET exists?](https://stackoverflow.com/questions/12019684/how-to-verify-if-get-exists) – ADyson Aug 18 '20 at 08:01

1 Answers1

0
<?php
if(isset($_GET['name'])){
echo "<h1>Hello " . $_GET["name"] . "</h1>";
} 
if(isset($_GET['age'])){
echo "<h1>You are " . $_GET["age"] . " years old </h1>";
}
?>
toby Toby
  • 16
  • 4