Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics PHP on the Web HTML with PHP

Explanation of Quiz Question: "When you view the source code of a PHP file in your browser, what should you see? "

Quiz Referenced: https://teamtreehouse.com/library/php-basics-2/php-on-the-web/html-with-php

One of the quiz questions asks: "When you view the source code of a PHP file in your browser, what should you see? "

The answer possibilities are:

Only HTML outside the PHP code block

Any HTML or PHP within the file

Only HTML output by the PHP interpreter

Only HTML outside the PHP code block or output by the PHP interpreter

Wouldn't the answer be that since the HTML file is now a PHP file all of the source code is "HTML" since the server returns HTML from the PHP code that it receives? Therefore, to me the answer should be: Only HTML output by the PHP interpreter. I know that's not the answer that the quiz is looking for.

However, I also wanted to also ask would this answer change to: "Any HTML or PHP within the file" if we looked at our code through the Chrome Developer tools? Since we'd be looking at the "raw" code itself...correct?

1 Answer

"Only HTML outside the PHP code block or output by the PHP interpreter"

Let's say your name_of_file.php file has the following:

<h3>Greeting</h3>
<?php echo "hello"; ?>
<p>That's all</p>

As the PHP interpreter on the server looks through the file, it's looking for the php tags, and processing them. So, it skips the <h3>Greeting</h3> since it's not PHP. There's nothing for the PHP interpreter to interpret (i.e. nothing for it to do).

When it reaches the 2nd line, the PHP interpreter sees PHP tags and starts interpreting. After it's done interpreting, it moves on to the 3rd line. On this 3rd line of <p>That's all</p>, there is nothing to interpret.

When it reaches the end of the file, it sends it back to the client's browser.

The client's browser now has a file that had either HTML tags or text. There's no PHP (like <?php echo "hello"; ?>) anymore. It's been interpreted into HTML and/or text.

So, lines 1 and 3 of the code in the original file had HTML or text only. That is the "Only HTML outside the PHP code block".

Line 2, that is interpreting PHP as HTML and/or text, is the "output by the PHP interpreter"