3

I've programmed for many years now in c++/c/python but never did a project in PHP, which I will be doing soon.

1- I find the usage of echo to be somewhat annoying, especially for printing long stuff. For example, if I have a function which wants to echo a redundant part of a website, such as the header or a nav menu, what approach would be better.

<?php
    function PrintHeader() {
        [some php stuff, mysql queries, etc]
?>
        some HTML code
        more HTML code
<?php
        [some closing php stuff]
    }
?>

Is it better than

<?php
    function PrintHeader() {
        [some php stuff, mysql queries, etc]

        echo 'some HTML code'
        echo 'more HTML code'

        [some closing php stuff]
    }
?>

I find somewhat the first approach to be, to me, simpler, because you can modify the HTML code more easily without the echos. I tested it, it works, but is it a good approach? Is it valid?

2- Is it considered a good programming practice to use a function to echo a redundant part of a website, rather than copy/pasting the redundant code accross all PHP files?

These questions might sound obvious to some, but I have not much experience about code management in PHP. I always liked to reduce code redundancy as much as possible; it's easier to maintain.

Yannick
  • 830
  • 7
  • 27
  • 2
    All are valid approaches, depends on what you're comfortable with. Also, look at [heredoc](http://stackoverflow.com/questions/5673269/what-is-the-advantage-of-using-heredoc-in-php) – Jay Blanchard Apr 13 '15 at 13:27
  • 1
  • Oh sorry, just edited. – Yannick Apr 13 '15 at 13:41
  • 2
    Best practice is to separate PHP from HTML. Hence there are template engines. Imagine a team of Front-End and Back-end developers. For front-end developers the backend should not matter, they should only care about an html file they can work with. They probably do not even know PHP, Ruby or Python or whatever is used in the backend. They might even destroy functionality because of some quotes or any other reason. – Richard Apr 13 '15 at 13:45
  • 1
    I disagree with the suggestions of using a template engine. The only valid reason for a template engine is that you don't want to allow executable php in the views because you don't trust the template designers. It doesn't matter if the template designer has to learn `foreach` vs `%foreach%` they'll have to learn the template language as well and their logic operators and functions. If the fronted guy can't pick-up a few common php or ruby functions I wouldn't hire them, they are simply not qualified for the job then - period. It's not 1998 any more and a template engine is just overhead. – floriank Apr 13 '15 at 14:16

2 Answers2

1

first of all, nowadays people often use template files which are later parsed by PHP. Often are template languages like Twig for example used.

  1. its more user preference in my opinion, but I definetly prefer the 1st variant because its a lot more readable :).

  2. You should definetly put redundant parts of your website into a template and just include it if you need it :).

Xatenev
  • 6,383
  • 3
  • 18
  • 42
  • Templates? You see, somewhat, I want my code to be able to run in a shred hosting env. Does such env's usually allow Twig, I mean, generally, and templates (arent templates unsupported PHP extensions)? And how templates are better than pure PHP? I'll definitely inform myself about templates, but from your words, what advantage do they bring? – Yannick Apr 13 '15 at 13:43
  • Wlel, normally u have plain PHP inside ur templates. If you are using template systems you have access to variables, filters etc in your templates. A template file would be for example a header.html which contains all DOM for your header. Anyways, Template systems are not included in PHP by default, you will have to download plugins/extensions/modules in order to use those. You definetly don't need them, Plain PHP and HTML can be just as good. – Xatenev Apr 13 '15 at 13:45
1

Traditionally, this kind of thing is achieved in PHP by "requiring" a file that contains the content you need. e.g.

header.php:

<div>some html</div>

Your main file:

<?php
require 'header.php';
?>

This removes much of the need to embed HTML within your PHP. However, a more modern approach is to use a templating language, which allows you to house your HTML, along with simple display logic like branching and loops, outside of your PHP code entirely. Have a look at http://www.phptherightway.com/#templating for some more advice on both approaches. I'd strongly recommend you read all of phptherightway.com, in fact - it doesn't take long and will probably answer a lot of other questions you might have.

Jez
  • 2,384
  • 1
  • 17
  • 31