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 (Retired) PHP Conditionals & Loops Foreach Loops

Janos Antal
Janos Antal
1,451 Points

php in .html and vice-versa, how?

Hello,

I do not understand something with .html and .php. How to use both together in one file. E.g. given is the following code:

<!doctype html>
<html>

<body>
<ul>
<?php
    for($counter=0; $counter < 10; $counter++){
        echo "<li>" . $counter . "</li>";
    }

?>
</ul>
<section>
<h1>Foreach Loop</h1>
<?php
//this part is not finished
    $images = $array('email','linkedin','tel','twitter','person');

?>
<ul>
    <li><img src="images/email.png" alt=""></li>
</ul>
</section>
</body>
</html> 

My problem is the following. If I use this code in a .php file the img src does not seem to be working, the image does not show up on the page. If I insert this code into a .html file the picture shows up, but then the php does not work.

How to use then html and php together? In the video everything seems to be ok in the index.php file.

2 Answers

Kevin VanderWulp
Kevin VanderWulp
5,180 Points

There is a way to tell your Apache server to interpret file type .html the same as .php, but the PHP processor returns everything as html, so this shouldn't be a problem. I would recommend inspecting the image in the browser to see if anything is getting changed, but I can't see any reason why it would.

Janos Antal
Janos Antal
1,451 Points

Sorry Kevin, I am not sure if I perfectly understand your answer. Is it ok if the server interprets the .html as .php? I mean what is the most usual and accepted solution?

I definitely do not understand your second sentence. inspecting what exactly? If I rename the file to .php the php works, html not, if I rename to .html, the html works and the php not.

Kevin VanderWulp
Kevin VanderWulp
5,180 Points

The most accepted practice is to have all files containing PHP code to be in a .php file. I am not sure what is causing the problem you are experiencing, but it is not something that I've ever seen or heard of unless you have bad php syntax that is causing the html to not display correctly.

What I mean by inspecting is either viewing the source code or using the developer tools in your browser to inspect the code. Most modern browsers such as IE, Chrome, and Firefox, have an option when you right click on a page called inspect element. This will open of the developer's tool set and allow you to view and interact with the source code. Alternatively you can just view the source code without the tools by selecting View page source from the menu when you right click on the page.

Here is a course that explains chrome dev tools. You'll see similar tools available in firefox and safari. Inspectors and dev tools super helpful so I would run through this course :-)

If Firefox is your browser of choice and you've got to grips with this type of tool, you could look into downloading firebug.

It's totally not necessary, but it will make life a whole lot easier in the near future!

When a file has a .php extension, you tell the server "Hey! This file has php in it. Parse this file please." The server knows that all php is going to be inside php tags:

<?php
// Yo, I'm php. 

?>

so anything that isn't inside php tags = text. If that text, which may be itself or inside html markup:

<!doctype html>
<html>

</html>

then the browser will interpret that correctly as html.

Because you have php in your file, you must have an extension of .php

Syntax errors in php cause things to break - and I've spotted one here:

<?php

$images  = $array('email','linkedin','tel','twitter','person');

// should not have dollar sign in front of 'array'

$images  = array('email','linkedin','tel','twitter','person');

Fix the syntax error and see what happens.

Hope this helps!