I’ve been reading a book on Zend framework and there’s this HTML/PHP code section I can’t figure out. It’s contained in the VIEWS part of the MVC methodology:
<select name="genre">
<?php foreach ($this->genres as $genre) { ?>
<option value="<?php echo $genre ?>"><?php echo $genre ?></option>
<?php } ?>
</select>
The genre ($this->genres) refers to array('rock', 'r&b', 'country', 'rap', 'gospel', 'rock n roll', 'techno').
The code runs perfectly, producing a drop-down select menu, but I don’t understand how the second line is even legal, let alone work. How does the PHP code work beyond its enclosing tags?
PHP is an unusual (templated) language in this context. The parser actually considers everything between
?>and<?phpas being some weird kind of echo. It is ignored as part of the program code, although the parser does run (it just outputs it and skips it as part of program code).From the PHP manual:
This allows PHP to be used for numerous of things. You can’t just create dynamic HTML files with it, you can for example also create XML (although it’s a bit tricky to get the XML header right), text files, CSS files, etc., as long as the PHP interpreter is ran for that file, it will execute everything between
<?phpand?>as program code and the rest will be outputted as-is.