Solved

PHP code best practices

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

919 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

18 Experts available now in Live!

Get 1:1 Help Now