Solved

HTML PHP foreach loop of array not displayed to page.

Posted on 2014-01-08
12
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 83

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 110

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 110

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 83

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

705 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