Newbie problem with PHP and HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BuckeyeTheDog
Forum Newbie
Posts: 5
Joined: Wed Mar 16, 2011 9:19 pm

Newbie problem with PHP and HTML

Post 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.
jaceinla
Forum Commoner
Posts: 25
Joined: Thu Oct 14, 2010 12:57 pm

Re: Newbie problem with PHP and HTML

Post 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.
aIexi
Forum Newbie
Posts: 12
Joined: Wed Mar 16, 2011 10:46 pm

Re: Newbie problem with PHP and HTML

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Newbie problem with PHP and HTML

Post 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/
aIexi
Forum Newbie
Posts: 12
Joined: Wed Mar 16, 2011 10:46 pm

Re: Newbie problem with PHP and HTML

Post by aIexi »

Oh ok >< thanks jonah. good to know!
BuckeyeTheDog
Forum Newbie
Posts: 5
Joined: Wed Mar 16, 2011 9:19 pm

Re: Newbie problem with PHP and HTML

Post 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?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Newbie problem with PHP and HTML

Post 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.
BuckeyeTheDog
Forum Newbie
Posts: 5
Joined: Wed Mar 16, 2011 9:19 pm

Re: Newbie problem with PHP and HTML

Post 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?
BuckeyeTheDog
Forum Newbie
Posts: 5
Joined: Wed Mar 16, 2011 9:19 pm

Re: Newbie problem with PHP and HTML

Post 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 />
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Newbie problem with PHP and HTML

Post 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).
Post Reply