I'm new to the PHP game and my problem is one of understanding so apologies for the 101 question.
I have an index.php in a sub-folder on my site. I know that I can pass an argument to the page via the URL.
https://example.com/subfolder?id=1234
If someone navigates to
https://example.com/subfolder
without the argument, how do I supply the default 4321?
The code I've gleaned from other Stack Overflow questions is
<?php
$id = (isset($_GET["id"])) ? $_GET["id"] : "4321";
echo "<some html>" . $id . "<some more html>";
?>
I've also tried
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
} else {
$id = "4321";
}
echo "<some html>" . $id . "<some more html>";
?>
which I believe is just a different way of writing the same thing.
The code pulls the argument from the URL when it's supplied and the page works fine, but it doesn't supply the default when the argument isn't supplied. I suspect it's got something to do with the id variable not being defined if it's not in the URL but I don't know.
Thanks muchly if you can help.
EDIT: Just in case you're right James, I'm including the actual echo line that I'm using (sans domain). Everything else is the same as I've typed it above.
echo '<iframe width="70%" padding-bottom="20px" height="900" scrolling="yes" frameborder="no"
src="https://example.com/eventlist/415c564448271d0f1504/?point_of_sale_id=' . $id . '"></iframe>';
EDIT: And when I view the page source, this is what's returned from the PHP
<iframe width="70%" padding-bottom="20px" height="900" scrolling="yes" frameborder="no"
src="https://example.com/eventlist/415c564448271d0f1504/?point_of_sale_id="></iframe>
This was the code I got to work, I had to add in an extra step.
<?php
$id = $_GET["id"];
$linkId = (isset($id)) ? $id : "4321";
echo "<some html>" . $linkId . "<some more html>";
?>
I'd be interested to know if anyone can work out why the original didn't work though.