Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP best practices? [closed]

Tags:

php

What is a good way to remove the code from display pages when developing with PHP. Often the pages I work on need to be editted by an outside person. This person is often confused by lots of blocks of PHP, and also likes to break my code.

I've tried moving blocks of code out into functions, so now there are functions spread out all throughout the HTML now. As some pages become more complex it becomes a program again, and processing POSTs are questionable.

What can I be doing better in my PHP development?

like image 408
Jack B Nimble Avatar asked Aug 30 '08 22:08

Jack B Nimble


People also ask

Why is PHP still so popular?

It's versatile: One of the major benefits of PHP is that it is platform independent, meaning it can be used on Mac OS, Windows, Linux and supports most web browsers. It also supports all the major web servers, making it easy to deploy on different systems and platforms at minimal additional cost.

Why is PHP so fast?

PHP is much faster than other options (such as ASP) because of its faster load time as a result of its own memory space that removes most of the overhead in execution.


2 Answers

You don't need a "system" to do templating. You can do it on your own by keeping presentation & logic separate. This way the designer can screw up the display, but not the logic behind it.

Here's a simple example:

<?php 
$people = array('derek','joel','jeff');
$people[0] = 'martin'; // all your logic goes here
include 'templates/people.php';
?>

Now here's the people.php file (which you give your designer):

<html> 
<body>
<?php foreach($people as $name):?>
   <b>Person:</b> <?=$name?> <br />
<?php endforeach;?> 
</body>
</html>
like image 165
lo_fye Avatar answered Oct 08 '22 13:10

lo_fye


Take a look at how some of the popular PHP frameworks use templating. Examples include cakePHP, Zend Framework, and Code Igniter. Even if you are not going to base your site on these frameworks, the template design pattern is a good way to keep php code away from your web designers, so they can focus on layout and not functionality.

like image 33
Christian Oudard Avatar answered Oct 08 '22 12:10

Christian Oudard