?
Solved

HTML PHP foreach loop of array not displayed to page.

Posted on 2014-01-08
12
Medium Priority
?
380 Views
Last Modified: 2014-07-07
I've copied code from 3wSchools.com in an effort to learn html and php.  if I run the script below all I get on the screen is:     "; } ?>


      <html>
      <head>
      <body>

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

      </body>
      </html>



What should be displayed is :

red
green
blue
yellow


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.
0
Comment
Question by:Steve7423
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39766137
Save the page with a .php extension not .html
And this is assuming you are running on Apache or IIS with PHP installed.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39766265
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.

WAMP: http://www.wampserver.com/en/

XAMPP: http://www.apachefriends.org/en/xampp.html
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39766553
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:rinfo
ID: 39767565
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.
0
 
LVL 9

Expert Comment

by:rinfo
ID: 39767567
To add further
You must have file in document root and run file in browser using localhost/path-to_file/filename
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39767909
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
0
 
LVL 9

Expert Comment

by:rinfo
ID: 39767960
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.
Thanks
0
 

Author Comment

by:Steve7423
ID: 39786729
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.
0
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 39786757
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
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39786760
You can use the Microsoft PHP installer instead to use it on IIS.  http://www.microsoft.com/web/platform/phponwindows.aspx
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question