Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Where to place the PHP code
Old 05-30-2010, 09:12 PM Where to place the PHP code
ericson3000's Avatar
Experienced Talker

Posts: 40
Name: Bob Davis
Location: Los Angeles, CA
Trades: 0
I know that you can put PHP code inside HTML so long as it is enclosed in the <?php ... ?>

Also, you can keep the code in an external PHP document.

However, I have been told that PHP code can be placed in a document before the <!DOCTYPE>. Is this true, or like JavaScript, should it simply be placed in the <head>. The code will be used for a form on the page.
ericson3000 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-30-2010, 09:27 PM Re: Where to place the PHP code
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
All client side code (HTML, javascript, CSS) is completely distinct from server side code (PHP in this case). It does not matter if you put PHP code before or after the doctype, because PHP is not aware of any particular markup that you are using, just as client side code is completely unaware of server side code.

HTML is the output of your PHP script.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-30-2010, 09:29 PM Re: Where to place the PHP code
ericson3000's Avatar
Experienced Talker

Posts: 40
Name: Bob Davis
Location: Los Angeles, CA
Trades: 0
Thanks for the clarification. Appreciate it.
ericson3000 is offline
Reply With Quote
View Public Profile
 
Old 05-31-2010, 09:32 AM Re: Where to place the PHP code
Experienced Talker

Posts: 48
Name: Websys_seo
Location: India
Trades: 0
Quote:
Originally Posted by ericson3000 View Post
I know that you can put PHP code inside HTML so long as it is enclosed in the <?php ... ?>

Also, you can keep the code in an external PHP document.

However, I have been told that PHP code can be placed in a document before the <!DOCTYPE>. Is this true, or like JavaScript, should it simply be placed in the <head>. The code will be used for a form on the page.
PHP is a server site script while HTML or Javascript is client side scripting language. So it really doesn't matter where you are placing your script. But as a good programmer you should always try to make best use of compiler and interpreter. So the pages where we require session check or some security check then it is better to include the PHP codes in the beginning, so that in case of failure checking the value the url can be redirected without interpreting the HTML version below it.

So it's up0 to you where you would like to use <?php ?>
__________________

Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Arings is offline
Reply With Quote
View Public Profile Visit Arings's homepage!
 
Old 05-31-2010, 09:46 AM Re: Where to place the PHP code
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
So it's up0 to you where you would like to use <?php ?>
That's a very broad statement and as such is incorrect..


IF the PHP code is delivering output as content, it MUST be placed in the document where the content is intended to be displayed.
If the PHP code is to deliver a header response to the user agent (redirect or similar) it MUST be before any output is sent to the user agent.

Whatever the PHP script is actually doing it has to be at an appropriate location in the document.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-31-2010, 10:14 AM Re: Where to place the PHP code
Novice Talker

Posts: 12
Name: joyhitech
Trades: 0
In my experience i can place php code before <!DOCTYPE> it well
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
joyhitech is offline
Reply With Quote
View Public Profile
 
Old 05-31-2010, 11:02 AM Re: Where to place the PHP code
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Quote:
Originally Posted by joyhitech View Post
In my experience i can place php code before <!DOCTYPE> it well
As chris has said, You CAN place it where the hell you like.

However, It can only be placed in certain places depending on what you want the outcome of your script to do...

So the answer in a broader sense is, Yes and No
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 05-31-2010, 12:44 PM Re: Where to place the PHP code
Marik's Avatar
Skilled Talker

Posts: 99
Trades: 0
Sometimes you can tell when PHP (or some other server side code) has been placed before the doctype when you view the source of a page and find that the HTML starts a few lines below one or more empty lines.

Even though PHP code as a server side language will never display itself in a page's source code it will still take up empty lines in the source (if it's the type of code that doesn't have any output to occupy it's place).
Marik is offline
Reply With Quote
View Public Profile
 
Old 05-31-2010, 12:48 PM Re: Where to place the PHP code
Marik's Avatar
Skilled Talker

Posts: 99
Trades: 0
Quote:
Originally Posted by chrishirst View Post
That's a very broad statement and as such is incorrect..


IF the PHP code is delivering output as content, it MUST be placed in the document where the content is intended to be displayed.
If the PHP code is to deliver a header response to the user agent (redirect or similar) it MUST be before any output is sent to the user agent.

Whatever the PHP script is actually doing it has to be at an appropriate location in the document.
Agreed, and of course the obvious chronological order of commands must be followed for a script to work properly. This won't work:

PHP Code:

echo $name;

$name 'Mike'
Marik is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Where to place the PHP code
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.56066 seconds with 12 queries