Scripting Best Practices & Tidy Code

I came into PHP from C-based & .NET programming, and one thing that struck me as particularly tough is the merging of the various syntaxes and languages inherent in web dev. As such, my first few scripts are getting very messy / unreadable, and so I wondered if there were any best practices or standard methods the more seasoned folks adhere to, when it comes to the melting pot of php, html, MySQL, js and whatever else you may use? (Specifically with the aim of keeping your code tidy & easy to follow.)

Like, I know header info has to go before any html is sent to the browser.
I have separate html scripts for the repeated page elements like a navigation bar, which I include as includes().
I have a separate DB connection script.

Do you have anything else useful, to standardize the logic of how scripts are constructed? I am improvising badly at the moment, and need to rein it in and clean my house, so to speak.

Thanks!

It sounds like your on the right path with including include files for commonly used code. The structure of a php page should be tailored to the people who will be ‘touching it’. If the only people touching the pages are those with good client side and server side code knowledge, then php code integration with client side code shouldn’t be a problem.

If you are working on or plan to work with someone who doesn’t have good server side knowledge (i.e. html designers), then you may want to employ a framework for separating the server side logic from the client side so they can do their job easily. I usually recommend ‘Smarty’ to accomplish this task as it’s easy to comprehend and setup. I hope this helps.

Sponsor our Newsletter | Privacy Policy | Terms of Service