Solved

PHP code best practices

Posted on 2014-09-19
4
449 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
  • 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 108

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now