HTML PHP foreach loop of array not displayed to page.

I've copied code from in an effort to learn html and php.  if I run the script below all I get on the screen is:     "; } ?>


$colors = array("red","green","blue","yellow");
foreach ($colors as $value)
   echo "$value <br>";


What should be displayed is :


this is my first week with html and I'm completely bewildered as to why this isn't working.  I've tried multiple different possibilities for arrays and loops and nothing works.  

is there a problem with the code or my environment?  what would cause this?  why can I not print out elements of an array?

the object of the game is to loop through an array and display it in a table, however first I have to get an array to display, and it won't !

Help is appreciated.
Who is Participating?
GaryConnect With a Mentor Commented:
You can either install PHP on IIS
Or remove IIS and install XAMPP or WAMP - these will install Apache and PHP for you.
You cannot run both IIS and XAMPP/WAMP without changing the default port for one or the other, else they will both be listening on port 80 and that won't work
Save the page with a .php extension not .html
And this is assuming you are running on Apache or IIS with PHP installed.
Dave BaldwinFixer of ProblemsCommented:
To add to what Gary said, PHP must be run thru a web server that has it installed.  PHP does not run in the web browser but on the web server to generate a page for you.  Pro versions of Windows have Microsoft IIS available or you can install WAMP or XAMPP on Windows.  Most Linux machines will install a LAMP stack for you which includes Apache, PHP, and MySQL.


Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Ray PaseurCommented:
There is nothing wrong with the examples at W3Schools, but there are a lot of other really good learning resources that should not be overlooked.
Are you sure you have the files in the server document root directory.
Alternately  you can run it in php command line , from either php installation directory or from anywhere if you have added path to php installation in the environment variables.
Of course you must have extension changed to .php instead of html.
To add further
You must have file in document root and run file in browser using localhost/path-to_file/filename
Ray PaseurCommented:
Of course you must have extension changed to .php instead of html.
Technically speaking, this is not true.  You must have the PHP parser associated with the file extension.  The script could be .foo and that would be OK, as long as the files with the .foo extension got parsed through PHP.  In my sites I parse .php and .html through the PHP parser.  There is a negligible performance penalty and a great benefit in flexibility.

Here is the line from .htaccess that enables .html and .htm files to contain PHP scripts.

AddType application/x-httpd-php5 .html .htm .php

Open in new window

Best to all, ~Ray
Hello Ray
What you says is technically  right  and i stand corrected.
My assumption was answer was sought by some one who might not have
been that  php-apache-savvy and answer was an easy way out , wish i would
have added why it is important to have .php extension.
Steve7423Author Commented:
Wow, thanks guys, tons of info to consider.  Incidentally I'm using Windows 7.
I'm running this on my local machine.  all html stuff works but for some reason anything dealing with php doesn't work.  

I'm new to php as I was just tossed in to it, and I have no idea what I'm doing; I'm sure you guys know the feeling.  

If I have IIS running on the local machine do i still require WAMP ?  

Are there special files required in the file folder ?

I might a bit of hand holding here.
Dave BaldwinFixer of ProblemsCommented:
You can use the Microsoft PHP installer instead to use it on IIS.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.