Page 1 of 1
Newbie problem with PHP and HTML
Posted: Wed Mar 16, 2011 9:39 pm
by BuckeyeTheDog
Newbie here. Thanks for your help.
I am having incorrect output on the sample code from beginner's books. This is the code...
Code: Select all
<html>
<head></head>
<body>
<ul>
<?php
$footballTeam = array('Chiefs', 'Bengals', 'Redskins', 'Chargers');
foreach ($footballTeam as $item)
{
echo "<li>One football team's name is $item";
}
?>
</ul>
</body>
</html>
The output on the html page is...
One football team's name is $item"; } ?>
Instead of going through the values in the array. I am having this problem with my output pretty much anytime I have php inside of an HTML file...
Thanks for any help you can give.
Re: Newbie problem with PHP and HTML
Posted: Wed Mar 16, 2011 10:11 pm
by jaceinla
Code: Select all
echo "<li>One football team's name is " . $item . "</li>";
That's one way to do it...double check your book for double quotes vs single quotes, they affect vars differently.
Re: Newbie problem with PHP and HTML
Posted: Wed Mar 16, 2011 11:00 pm
by aIexi
Hi there!
I'm still pretty new to PHP, but I think I know your problem, so I'll do my best to help.
When you do an echo, you can do 1 of 3 things.
1. You can echo a variable. In this case "$item" is your variable. it woudl look like this [ echo $item; ]
2. You can echo Text or HTML. it would look like this. [ echo "this is where your HTML goes";]
3. You can enter HTML AND a variable. it would look like this: [ echo "hello, the item is" . $item . "now you know the item's name"; ]
Now, when I learned this concept, I was like OMGOSH what?!?! Those "." things randomly before and after item pretty much, to my understanding, seperate the HTML from the $variable. not sure why, but just got with it.
So your code would look something like this:
Code: Select all
echo "<li>One football team's name is" . $item . ".";
Also, looking through your code, you never state what $item is. When you run this, I don't believe $item will show as anything, as the $item variable doesn't seem to have been declared!
Anyway, I hope this helps! Forgive me if my answer is useless. I'm new as well
AIexi
Re: Newbie problem with PHP and HTML
Posted: Wed Mar 16, 2011 11:28 pm
by Jonah Bron
Both good answers, but unfortunately wrong.
@jaceinla: yes, he's missing the closing tag, but that's only part of the problem.
@aIexi: actually, with double-quoted strings, variables can be placed directly in the string (
see here). And $item is in fact defined (
see here).
The output on the html page is...
One football team's name is $item"; } ?>
That indicates that the PHP is not being run. Do you have WAMP (Windows, Apache, MySQL, PHP) installed? If not, you can get it here:
http://www.wampserver.com/
Re: Newbie problem with PHP and HTML
Posted: Wed Mar 16, 2011 11:33 pm
by aIexi
Oh ok >< thanks jonah. good to know!
Re: Newbie problem with PHP and HTML
Posted: Thu Mar 17, 2011 12:01 pm
by BuckeyeTheDog
I have XAMPP installed on a Mac running Snow Leopard 10.6.6... It's running the straight PHP stuff fine. And when Ive uploaded the file to my server, I still have the same incorrect HTML output.
Any problems with that?
Re: Newbie problem with PHP and HTML
Posted: Thu Mar 17, 2011 12:58 pm
by Jonah Bron
You mean a remote server? Are you sure it's exactly the same? Try viewing the source of the page from your browser, and post it here.
Re: Newbie problem with PHP and HTML
Posted: Thu Mar 17, 2011 1:26 pm
by BuckeyeTheDog
Good idea, Jonah. I still see the PHP in the outputted HTML... hmmm...
Code: Select all
Good idea, Jonah... Here is the outputted HTML from my localhost... I see the PHP still in there...
<html>
<head></head>
<body>
<ul>
<?php
$footballTeam = array('Chiefs', 'Bengals', 'Redskins', 'Chargers');
foreach ($footballTeam as $item)
{
echo "<li>One football team's name is $item";
}
?>
</ul>
</body>
</html>
This is also the code from my server at hmkdev.com/ctec/php/arrayFive.html . I am successfully running a Wordpress site and the PHP for that is working fine.
Code: Select all
<html>
<head>Hello World</head>
<body>
<ul>
<?php
$footballTeam = array('Chiefs', 'Bengals', 'Redskins', 'Chargers');
foreach ($footballTeam as $item)
{
echo "<li>One football team's name is $item";
}
?>
</ul>
</body>
</html>
What do you think?
Re: Newbie problem with PHP and HTML
Posted: Thu Mar 17, 2011 1:36 pm
by BuckeyeTheDog
Oh, and you'll also see my server successfully running similar code as a PHP file (no HTML) in hmkdev.com/ctec/php/arrayFour.php.
Heres' the PHP for that
Code: Select all
<?php
$football = array('Chiefs', 'Bengals', 'Redskins', 'Chargers');
foreach ($football as $item)
{
echo "$item";
echo "<br />";
}
?>
This successfully outputs this source html to the browser
Chiefs<br />Bengals<br />Redskins<br />Chargers<br />
Re: Newbie problem with PHP and HTML
Posted: Thu Mar 17, 2011 3:36 pm
by Jonah Bron
Okay, the problem is that you set the extension of the file as .html. Change it to .php and it should work fine (this is evident by the fact that arrayFour.php works, but arrayFive.html does not).