<p>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.</p>

<p>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.</p>

<p>What can I be doing better in my PHP development?</p>

[{'answer_id': 36421, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 0, 'selected': False, 'text': "<p>There's a lot that can be said on this topic but a very basic starting point would be to move as much code as possible out into separate files and then use include statements.</p>\n"}, {'answer_id': 36425, 'author': 'Christian Oudard', 'author_id': 3757, 'author_profile': 'https://Stackoverflow.com/users/3757', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 36428, 'author': 'different', 'author_id': 3654, 'author_profile': 'https://Stackoverflow.com/users/3654', 'pm_score': 0, 'selected': False, 'text': '<p>I usually use includes, as they can be very useful for organising and grouping functions together. Also, comment your code. There\'s nothing worse than for someone else to see your work and not know why you\'ve done this. Naming variables and functions sensibly can go a long way too - for example:</p>\n\n<pre><code>$userName = "John Doe";\n$dateOfBirth = "04/02/1982";\n\nfunction calculateUserAgeFromBirth($userName, $dateOfBirth)\n</code></pre>\n\n<p>Naming variables like this also helps minimise comments about <em>what</em> your code actually does.</p>\n'}, {'answer_id': 36429, 'author': 'Anonymoose', 'author_id': 2391, 'author_profile': 'https://Stackoverflow.com/users/2391', 'pm_score': 2, 'selected': False, 'text': '<p>Does the outside person need to edit the logic, or just the display (HTML)?</p>\n\n<p>If it\'s the latter case, check out the <a href="http://www.smarty.net/" rel="nofollow noreferrer">Smarty</a> template engine.</p>\n'}, {'answer_id': 36444, 'author': 'lo_fye', 'author_id': 3407, 'author_profile': 'https://Stackoverflow.com/users/3407', 'pm_score': 5, 'selected': True, 'text': '<p>You don\'t need a "system" to do templating.\nYou can do it on your own by keeping presentation &amp; logic separate.\nThis way the designer can screw up the display, but not the logic behind it.</p>\n\n<p>Here\'s a simple example:</p>\n\n<pre><code>&lt;?php \n$people = array(\'derek\',\'joel\',\'jeff\');\n$people[0] = \'martin\'; // all your logic goes here\ninclude \'templates/people.php\';\n?&gt;\n</code></pre>\n\n<p>Now here\'s the people.php file (which you give your designer):</p>\n\n<pre><code>&lt;html&gt; \n&lt;body&gt;\n&lt;?php foreach($people as $name):?&gt;\n   &lt;b&gt;Person:&lt;/b&gt; &lt;?=$name?&gt; &lt;br /&gt;\n&lt;?php endforeach;?&gt; \n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n'}, {'answer_id': 36532, 'author': 'Jack B Nimble', 'author_id': 3800, 'author_profile': 'https://Stackoverflow.com/users/3800', 'pm_score': 1, 'selected': False, 'text': "<p>I think I'd like to stay away from an unweildy framework. Just some approach I can use that generally makes the pages more readable with cleaner code. </p>\n\n<p>Stack Overflow wants me to decide which answer is best, when best is a subjective opinion. Who is to say what the 'best' practice is. </p>\n"}, {'answer_id': 36867, 'author': 'analytik', 'author_id': 2277364, 'author_profile': 'https://Stackoverflow.com/users/2277364', 'pm_score': 1, 'selected': False, 'text': '<p>If you decide to continue using functions, you can get some inspiration from WordPress. You can probably reduce the "program" to a minimum by making templates more granular.</p>\n\n<p>Also, good tools (i.e. HTML editors) can help designers ignore your PHP and work on the design without breaking the code. (But I have no suggestions, sorry.)</p>\n\n<p>The other way to some things is to create own template system instead of SMARTY, but it would probably take too long to create a working system to satisfy your needs that would go past just a replacing something like %%VARIABLE%% with a text.</p>\n\n<p>Our company uses SMARTY and even with a lot of code in templates, designers know how to work with it. For simple CMS sites we use ExpressionEngine, which uses HTML-like tags for inserting logic into templates.</p>\n'}, {'answer_id': 43927, 'author': 'Sam McAfee', 'author_id': 577, 'author_profile': 'https://Stackoverflow.com/users/577', 'pm_score': 3, 'selected': False, 'text': '<p>It sounds to me like you need to begin implementing what is known as "separation of concerns" across your application generally. The examples folks give about templating, in response to your specific complaint about page editors breaking your code, are important, but represent just one example of this tactic. As your program gets larger and more complex it becomes harder to modify and debug--even if your designer is not breaking your code.</p>\n\n<p>Probably the most common separation is a three way split between data, logic and presentation as described in the design pattern Model-View-Controller (MVC). You do not need a full blown <a href="http://en.wikipedia.org/wiki/Model-view-controller" rel="noreferrer">MVC</a> framework in place to implement the same basic principles. The idea is simply to encapsulate code that deals with your data (model) in one place, the code that presents this data to the user (view) in another. You tie that code together with code that is only concerned with presenting the right data to the right user at a the right time (controller).</p>\n\n<p>From your description, it sounds like you have right now is a <a href="http://www.martinfowler.com/eaaCatalog/transactionScript.html" rel="noreferrer">Transaction Script</a> pattern, where you have a php file "dothis.php" that is loaded in the browser, and all the function definitions and HTML for the display are together. You already have functions, so you are already beginning to encapsulate pieces of logic.</p>\n\n<p>The way I would approach this would be, in keeping with the other answers here about templating, is to remove all of the HTML into another file only referencing simple PHP variables and maybe some loops (but as little conditional switching as you can). That will make the template easier to read and harder to break. When your page editor wants to modify the layout, give them THAT file.</p>\n\n<p>You then separate all of your data access functions to another file, ideally creating a class (or several classes, depending on how complex your data is and how frequently you need to reuse it). </p>\n\n<p>At this point your "dothis.php" has been stripped down to maybe some configuration code (which you can separate out to an include, and some authentication code (which you can separate out to its own class), and is only calling the data access functions, and calling the included template file. Your controller itself is therefore greatly simplified and easier to manage.</p>\n'}, {'answer_id': 43981, 'author': 'David McLaughlin', 'author_id': 3404, 'author_profile': 'https://Stackoverflow.com/users/3404', 'pm_score': 2, 'selected': False, 'text': '<p>I would highly recommend reading the book <a href="http://www.manning.com/reiersol/" rel="nofollow noreferrer">PHP In Action</a>. It takes you through abstracting your database connections, templating systems and all the other basics of a web application. If every PHP developer read this book then the language would have a much better reputation.</p>\n\n<p>It also has chapters on refactoring, unit testing and the MVC control pattern. </p>\n'}]
['https://Stackoverflow.com/questions/36417', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3800/']