Jump to content
Sign in to follow this  
SpeedCrazy

PHP fail.

Recommended Posts

Hey guys,

Could someone please take a look at this code and tell me what is wrong with it. Its driving me batty.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtm11/DTD/xhtml11-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>serverInputTable</title>
</head>
<body>
	<?php 
		print "<table> \n";

		foreach ($_SERVER as $key => $value){
			print <<<HERE
			echo "<tr><td>$key</td><td>$value</td></tr> \n"


HERE;
		} //end foreach
		print "</table> \n";
	?>
</body>
</html>

It should display server info in a table. I got a <dl> version to work but i cant get <table> to work.

Any ideas?

Thanks

Share this post


Link to post
Share on other sites

I'm on the road right now so it's hard for me to test your code. Just as a suggestion I would use "echo" over "print". W3 Schools says that

Tip: The print() function is slightly slower than echo()
.

 

It doesn't look like you closed your echo statement in your foreach loop.

 

What are the "<<<HERE" and "HERE" for in the code.

 

I'll try and take a closer look at it later.

Share this post


Link to post
Share on other sites

I use print because it can do everything that echo can and more so until i need speed. I am just going to stick to print for ease of use.

 

As for closing the echo statement i am totally confused. My textbook left it as:

                        foreach ($_SERVER as $key => $value){
                               print <<<HERE
                               "<tr><td>$key</td><td>$value</td></tr> \n"

I added the echo because the error code indicated that line.

 

The heredoc is so i can switch between html and php.

 

Error code is as follows:

\n"; foreach ($_SERVER as $key => $value){ print <<$key$value \n" HERE; } //end foreach print " \n"; ?> 

Share this post


Link to post
Share on other sites

First off. What problems do you have with your code? Does it throw an exception? Does it display incorrectly?

 

Second, can you tidy your code up so it is readable. The "HERE" bits make no sense. If you wish to write comments within the code, please use the proper comment syntax (Double forward slash, "//", in PHP. Or "/* comment here */".)

 

Thanks

Share this post


Link to post
Share on other sites

You need a semicolon at the end of the echo line. There is really no need for the HERE parts, or even the print statement.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtm11/DTD/xhtml11-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>serverInputTable</title>
</head>
<body>
<table>
 <?php                                                                        
    foreach ($_SERVER as $key => $value) {                                              
         echo "<tr><td>$key</td><td>$value</td></tr> \n";                              
    } //end foreach                                     
 ?>
</table>
</body>
</html>

Share this post


Link to post
Share on other sites

If you want to continue using the print and heredoc statement, you can do this:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtm11/DTD/xhtml11-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>serverInputTable</title>
</head>
<body>
<table>
<?php                                                                        
foreach ($_SERVER as $key => $value){
	print <<< HERE
	<tr><td>$key</td><td>$value</td></tr>
HERE;
} //end foreach                                     
?>
</table>
</body>
</html>

 

Personally, I would use the code in my first post. ;)

Share this post


Link to post
Share on other sites

A 3rd way that you could do this...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtm11/DTD/xhtml11-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>serverInputTable</title>
</head>
<body>
<table>
<?php                                                                        
foreach ($_SERVER as $key => $value){
?>
   <tr>
     <td>
     <?php echo $key; ?>
     </td>
     <td>
     <?php echo $value; ?>
     </td>
   </tr>
<?php
} //end foreach                                     
?>
</table>
</body>
</html>

Share this post


Link to post
Share on other sites

First off. What problems do you have with your code? Does it throw an exception? Does it display incorrectly?

 

Second, can you tidy your code up so it is readable. The "HERE" bits make no sense. If you wish to write comments within the code, please use the proper comment syntax (Double forward slash, "//", in PHP. Or "/* comment here */".)

 

Thanks

I gave the error in my second post. It does not show the table and gives this error.

\n"; foreach ($_SERVER as $key => $value){ print <<$key$value \n" HERE; } //end foreach print " \n"; ?>

 

What is wrong with my formatting? The indents are all out of whack having been copied from the .php but other than that it is neat and clear.

 

The heredoc is in there because my textbook told me to put it there. What is wrong with it?

I dont have any comments in the code.

 

D3, even with a semi-colon on that line it throws the same error.

Share this post


Link to post
Share on other sites

LOL, what a n00b i am, it works fine. I accidently saved it as a .html file not .php :doh:

Still im curious to see why you guys think heredoc is silly.

Share this post


Link to post
Share on other sites

LOL, what a n00b i am, it works fine. I accidently saved it as a .html file not .php :doh:

Still im curious to see why you guys think heredoc is silly.

haha :lol:

Share this post


Link to post
Share on other sites

Still im curious to see why you guys think heredoc is silly.

I've actually never even seen heredoc before. I looked it up and it seems to make what you're doing overly complicated. It seems best used for multi line strings.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...