|  | Cant get foreach alternate syntax to display results correct |  | |
| | | Jason Ourscene |  |
| Posted: Fri Jun 13, 2008 6:40 pm Post subject: Cant get foreach alternate syntax to display results correct |  |
I'm using the simplexml class to parse through some xml. when i use the standard foreach loop syntax, i get the expected results.
<?php foreach ($xml->item as $artist) { echo $artist->title "<br />"; }?>
Outputs: Kinky Matchbox Twenty
When i use the alternate syntax of: <?php foreach ($xml->item as $artist) > echo "$artist->title <br />"; <?php endforeach; ?>
It Outpusts: echo "$artist->title "; echo "$artist->title ";
Am i doing something wrong? |
| |
| | | Dikkie Dik |  |
| Posted: Fri Jun 13, 2008 6:40 pm Post subject: Re: Cant get foreach alternate syntax to display results cor |  |
| Quote: | When i use the alternate syntax of: ?php foreach ($xml->item as $artist) echo "$artist->title <br />"; ?php endforeach; ?
It Outpusts: echo "$artist->title "; echo "$artist->title ";
Am i doing something wrong?
|
Yes. You leave the echo statement to the HTML output, not to the PHP parser. So the alternative syntax version is:
<?php foreach ($xml->item as $artist): echo "$artist->title <br />"; endforeach; ?>
This works with almost all structures (except class, function and try).
Best regards. |
| |
| | | Jason Ourscene |  |
| Posted: Fri Jun 13, 2008 7:42 pm Post subject: Re: Cant get foreach alternate syntax to display results cor |  |
| |
|
|