Solved

PHP code best practices

Posted on 2014-09-19
4
473 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
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.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

736 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