0

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>
Baddie
  • 305
  • 1
  • 3
  • 11
  • 4
    Both of your code samples should work fine, so something is telling me there's another piece to this puzzle we're missing. – James Hunt Feb 09 '17 at 16:02
  • If these quotes `“` and `”` comes from your real code, change them to: `"` and try again. Only `'` and `"` are valid quotes in PHP. You might also want to check out: [How do I get PHP errors to display?](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – M. Eriksson Feb 09 '17 at 16:05
  • Thanks Magnus. I've amended the code. My code doesn't have the slanty ones. Don't really know how they snuck in there. – Baddie Feb 09 '17 at 16:15
  • https://3v4l.org/rG9nP – AbraCadaver Feb 09 '17 at 16:23

1 Answers1

0

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.

Baddie
  • 305
  • 1
  • 3
  • 11