PHP page scripts

Probably a very basic question… if your using the same php coding alot in a website is it better to just make its own php page and require it as and when you need it then copy and pasting the php onto each page - also with this method is it better security doing this? Any major advantages / disadvantages to this method?

I’ve just started doing it to make pages look a bit neater.

For me I do both, depends on how much code would be duplicated. For a small piece I create a function, for large code I do separate file and include it. The advantage is not having duplicate code to update. Update it in one place, it is updated everywhere. Remember the DRY principle. Don’t Repeat Yourself.

Procedurally, a single page using includes is best. Although, I have made a functions.php page before that I could call when needed.

To really blow your mind, the OOP was of doing it ( using __autoload ) makes it even better. I work with hundreds of classes , files, that can be the base of the next application I develop. So, the only thing that needs changing is the template files, the database connection, and any new classes that need to be written.

Sponsor our Newsletter | Privacy Policy | Terms of Service