Kevin
Kevin

Reputation: 1614

PHP include in middle of file

is this considered to be bad programming practice? I have includes in the beginning and end of my file in order to make templates. so it is like this

beginning.php
<html>
some stuff

end.php
some stuff
</html>

user accessed files
include beginning.php
some stuff
include end.php

Upvotes: 1

Views: 2518

Answers (3)

Niko
Niko

Reputation: 4448

No it's perfectly fine. You can include files in places wherever you need to.

You can even use conditional statements to include specific php files as opposed to others. For example, your index page can have a conditional statement that checks to see if the user is logged in. If the user is not logged in, include a php file with the login form. If the user is logged in, include a php file that displays the user's profile information instead.

The only thing you'd want to worry about is making your pages too spaghettied by trivially splitting things into different files if they don't need to be. But all in all, you're good to go.

Upvotes: 2

George Anthony
George Anthony

Reputation: 76

Using PHP this way is popular in design patterns such as the MVC, so you should be fine doing it. I wouldn't recommend it unless you have a clear structure to your projects because files can quickly become unorganised and messy.

Remember that PHP is generally finished executing before any HTML is outputted, so as long as it is logical to include PHP as-and-when, it should be fine.

Upvotes: 1

Hugo
Hugo

Reputation: 170

well, consider a designer point of view. will he/you find it difficult to make design changes to the file without affecting the server side implementation?

Upvotes: 0

Related Questions