I have completed most of PSET7, but I have hit a snag on the creation of my portfolio display. In my portfolio.php template, I have been attempting to use the following code snippet to loop through portfolio holdings of a particular user (per the spec), but I keep receiving an "unexpected end of file in ...portfolio.php on line 37" error whenever the page loads. I know know this error this usually means a missing [;, ), ?>, }, ]] and the interpreter is stuck waiting for the conclusion of some syntax... but I can't seem to figure out where.

Code snippet #1 (not working, "unexpected end of file" error):

<?php foreach ($shares as $share): ?>

            <td><?= $share["symbol"] ?></td>
            <td><?= $share["name"] ?></td>
            <td><?= $share["shares"] ?></td>
            <td><?= number_format($share["price_cur"], 2) ?></td>

<? endforeach ?>

I have spent a lot of time checking and re-checking my code for errors, but can't seem to nail down the issue. I eventually got the bellow code snippet to work while trying different implementations (options for different PHP / HTML syntax in the spec), but I have no idea why I get the "unexpected end of file" error using the first code snippet and NOT when using the second code snippet.

  • I am using the same variables in each snippet, so I believe I have ruled out some type of variable implementation error.
  • I have also looked at several examples online, and from what I can tell my PHP syntax is correct (even tried a few PHP code validator sites).
  • I have exchanged the code snippets with eachother exactly as displayed here (leaving the rest of the code--not included here--the same). Code Snippet #1: "Unexpected end of file" error. Code Snippet #2: portfolio.php works as intented, no errors.

2nd snippet, working code:

      foreach ($shares as $share)
              echo("<td>" . $share["symbol"] . "</td>");
              echo("<td>" . $share["name"] . "</td>");
              echo("<td>" . $share["shares"] . "</td>");
              echo("<td>$" . number_format($share["price_cur"], 2) . "</td>");

Maybe I have been looking at this too hard, but I am stumped. Can anyone explain to me why snippet #2 works, but snippet #1 doesn't? According to the problem spec, and much Googling, both syntax styles 'should' work just fine? (right?). What am I missing?

Since snippet #2 'works' as intended (per spec) I am well on my way to wrapping up PSET7, but not knowing what was is wrong with snippet #1 is really bothering me!

Thanks in advance for the help!


You would have figured this out eventually, but you have forgotten the php in <?php at the endforeach statement.

| improve this answer | |

Thanks @Bonga. Yes, that was it... been kicking myself for overlooking that for HOURS.

| improve this answer | |

PHP doesn't need the line in code 1, it sounds like you've spent too much time coding vba, where it is in fact used.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .