0

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?

0

3 Answers 3

1

PHP is an unusual (templated) language in this context. The parser actually considers everything between ?> and <?php as 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:

Everything outside of a pair of opening and closing tags is ignored by the PHP parser which allows PHP files to have mixed content. This allows PHP to be embedded in HTML documents, for example to create templates.
(...)
This works as expected, because when the PHP interpreter hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline - see instruction separation) until it hits another opening tag unless in the middle of a conditional statement in which case the interpreter will determine the outcome of the conditional before making a decision of what which to skip over.

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 <?php and ?> as program code and the rest will be outputted as-is.

Sign up to request clarification or add additional context in comments.

Comments

1

You can think of the sections between ?> and <?php tags as string arguments to echo (but without $variables or quotes interpretation).

So <?php echo 2;?>3 is equivalent to <?php echo 2; echo 3;?>.

Comments

0

It helps to remember that PHP stands for "PHP: Hypertext Preprocessor." In short, its primary job is to produce HTML output.

Everything inside the PHP tags is executed on the server side, and produces output as needed. This is added to everything that PHP ignores (anything outside the PHP tags, like <option value...></option> in your case.

You can think of it as though the PHP engine turns on and off each time it encounters a PHP tag.

<?php //this gets processed ?>
This is sent as output, unchanged
<?php // this gets processed ?>

and so on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.