Solved

PHP code best practices

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

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

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.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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