Solved

PHP code best practices

Posted on 2014-09-19
4
478 Views
Last Modified: 2014-09-19
Being new to PHP I would like how to organize my code better. I usually organize it the exact same page on every page. This are the parts I have recognized so far:

1) PHP code (example below)

<?php require_once( "../../webassist/security_assist/helper_php.php" ); ?>
<?php
if (!WA_Auth_RulePasses("Logged in to pcms2_users")){
      WA_Auth_RestrictAccess("../../logout.php");
}
?>

2) <html>  tag

3) <meta content>

4) <head>

5) <link .. to stylesheets>
6) <scrpt type ... >

7) </head>

8) <body>

9) All the 'body' code

10) </body>

11) </html>

Would that be accurate ?  or can I have the PHP code elsewhere as best practice, just looks weird to me that it is at the very top before my meta tags, etc.  I would usually have it between my meta tags and before the <head> content.

Any pointers for best practices are welcome.
0
Comment
Question by:amucinobluedot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 21

Assisted Solution

by:Randy Poole
Randy Poole earned 250 total points
ID: 40332325
You should spread it out if needed through the body, this is how you generate dynamic pages.  In the head if you need it to populate any of your in page JavaScript.  Remember if you name you called .js and .css files using a .php extension you can also dynamically alter those as well.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 40332337
Please see the answer here:
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28521658.html

There is a lot more to "best practices" than just the flow of linked PHP scripts, but some of the concepts are available in this article and the linked resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

As a general rule, I would suggest a common.php script that handles all of the standard startup stuff -- starting the session, acquiring the cookies, if any, connecting to the database and acquiring the active records indicated by the cookies.  The fewer "echo" statements you have, the more likely you will write debuggable and maintainable code.  Be sure to use the W3 validator frequently as you build out your HTML, CSS and JavaScript!
http://validator.w3.org/
0
 

Author Comment

by:amucinobluedot
ID: 40332347
Thank you  !
0
 

Author Closing Comment

by:amucinobluedot
ID: 40332350
Thank you  !
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question