Current location: Hot Scripts Forums » Programming Languages » PHP » General newbie code question


General newbie code question

Reply
  #1 (permalink)  
Old 01-13-08, 08:58 PM
Shelby's Avatar
Shelby Shelby is offline
Newbie Coder
 
Join Date: May 2004
Location: 3rd rock from the sun
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
General newbie code question

All examples I've seen so far place the PHP code in the <body> section of the page. Can PHP code go into the <head> section also? If you are going to work with an array, would you not put the array in the <head> section?

I know this is probably as simple of a question someone could ask, but I've never seen any examples (yet), placing code any where other than the <body>.
__________________
They have computers, and they may have other weapons of mass destruction. - Janet Reno
Reply With Quote
  #2 (permalink)  
Old 01-13-08, 11:09 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
PHP code can be placed in any part or portion of a page.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #3 (permalink)  
Old 01-13-08, 11:14 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Because a PHP code page produces and outputs a HTML document, the html/head/body tags are just data as far as PHP is concerned. Any block of PHP code goes wherever it logically needs to be to produce the content you want.

About the only real constraint is, any logic you have that causes a header to be sent to the browser must occur before any actual content is sent to the browser.

In the following, the PHP code is not in the <body> -

PHP Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Some Title</title>
</head>
<body>
<?php echo "Some content here<br />"?>
</body>
</html>
The line of php code in the above example is just one line of the overall HTML document. It just happens to be after the first 7 lines of in-line HTML.

In the above example, PHP code could just as easily echo all the lines shown, without having any in-line HTML that is outside of the <?php ?> tags.

You could also have a block of PHP echoing the title - echo "<title>Some Title</title>"; or you could have a block of php producing and echoing CSS or some javascript.

Think in terms of the overall document that is formed and sent to the browser.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #4 (permalink)  
Old 01-14-08, 10:15 PM
Shelby's Avatar
Shelby Shelby is offline
Newbie Coder
 
Join Date: May 2004
Location: 3rd rock from the sun
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, I thought you could probably put code in the <head>. But I learned a long time ago...assume nothing
__________________
They have computers, and they may have other weapons of mass destruction. - Janet Reno
Reply With Quote
  #5 (permalink)  
Old 01-14-08, 11:00 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Shelby View Post
Assume nothing
Very true lol. Php is processed before the document is sent, so you may place it anywhere in html, even between html tags, and it will still work. for example
PHP Code:

<HTML>
<H<? echo "EAD"?>>
will output
HTML Code:
<HTML>
<HEAD>
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Injecting a string into an If Statement ? nova912 PHP 4 07-21-06 02:04 PM
Newbie question about using PHP to get info from a MySQL Database nabnuts PHP 4 03-27-06 02:19 PM
newbie HTML question mrlocom HTML/XHTML/XML 8 04-11-05 12:07 PM
General php question for the xperts themanmathias PHP 1 04-12-04 02:47 PM
Newbie question thesus ASP 0 04-11-04 09:36 PM


All times are GMT -5. The time now is 07:33 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.