Solved

HTML PHP foreach loop of array not displayed to page.

Posted on 2014-01-08
12
324 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
12 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 82

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 108

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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

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 500 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 82

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now