1

Should I use

<?php
    if(!$user->is_logged_in()){
        echo '<p id="login">Click <a href="login">here</a> to log in</p>';
    }
?>

or

<?php
    if(!$user->is_logged_in()){
?>
    <p id="login">Click <a href="login">here</a> to login.</p>
<?php
    }
?>

? Why?

The first one will not have the HTML tags coloured so is less readable in code editors, but with the second one, it seems like it would be bad to take up three lines just to add a curly brace.

Which should I be using?

  • 8
    May I mention a third option: using templates? – Arseni Mourzenko Dec 14 '13 at 23:08
7

I have 6 years experience in PHP and I have actually had big issues with the readability and the maintenance of HTML code in strings. There is not any rule anywhere, but I think it is better to have the HTML separate from PHP.

  • It is easily readable in any editor (notepad++, sublime... e.t.c.). The HTML is colored and the PHP is colored. Even at the question you can see the difference :)
  • It is easier for a designer to change only the HTML code and not do any mistake in the PHP code accidentally.
  • It is easier if you want to move to a template library. For example, let's say that you want to use twig (http://twig.sensiolabs.org/). You can then more easily transform

this:

    <?php foreach ($users as $user) { ?>
        <div class="username">
            <div class="name"><?=$user->name?></div>
            <div class="lastname"><?=$user->lastname?></div>
        </div>
    <?php }?>

to this:

{% for user in users %}
    <div class="username">
        <div class="name">{{ user.name }}</div>
        <div class="lastname">{{ user.lastname }}</div>
    </div>
{% endfor %}
1

Don't use inverted logic to execute statements. It raises the change that you'll miss the single ! operator and think it does the opposite of what it's doing.

<?php
    if($user->is_session_empty()) {
        echo '<p id="login">Click <a href="login">here</a> to log in</p>';
    }
?>

Use a function for more clarity, and keep statements on the first indent.

<?php
    function foo($user) {
        if($user->is_logged_in()) {
            return;
        }
        echo '<p id="login">Click <a href="login">here</a> to log in</p>';
    }

    foo($user);
?>

For logic in templates use endif instead of { }.

<?php if($user->is_session_empty()): ?>
    <p id="login">Click <a href="login">here</a> to login.</p>
<?php endif; ?>

Readability of source code is an ambiguous guideline. What is readable for one person is not so readable for another. It's better to focus on maintainable code. That is code where you can navigate quickly, is kept clean and short. Objects, functions and files all have a single purpose.

There is no correct answer to your question.

0

Why use endif instead of {}? Most of the IDE mark appropriate starting brace { when the ending one is selected and oposite, which is not the case with endif sintax. Similar as with HTML starting and ending tags, when starting one is selected, the ending one is marked.

Say You have 1000 lines of HTML+php code, if condition starts on line 100 and ends on line 500, its good to have markers where it starts and where it ends, isnt so?

0

When I first met PHP, I decided that I will never mix HTML and PHP code in the same file. It was 13 years ago, and it was a good decision. Okay, you will find some echo("<b>$msg</b>"), or if ($debug) echo("<pre>"); print_r($result);, but that's all.

Use a templater, or you may even write a simple one, it's a good lession, even switching later to another one.

Did not ask, but here's another advice: use OOP. Finally, PHP supports the best OOP practices (was not available for long time), use it, design OOP, code OOP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy