E
E
ettaluni2021-06-30 14:15:30
PHP
ettaluni, 2021-06-30 14:15:30

Which style is better to use in noodles?

Good afternoon, I can not decide which is better, this:

<div class = "items">
    <?foreach ($items as $item):?>
        <span class = "items__item"><?=$item['name']?></span>
        <?if (isset($item['if'])):?>
            <span class = "if-span"><?=$item['if']?></span>
        <?endif;?>
    <?endforeach?>
</div>

Or this:
<div class = "items"><?
    foreach ($items as $item) {
        ?><span class = "items__item"><?=$item['name']?></span><?
        if (isset($item['if'])) {
            ?><span class = "if-span"><?=$item['if']?></span><?
        }
    }
?></div>

And I would like a detailed opinion why you use this. And of course I'm always happy with other new options.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2021-06-30
@ettaluni

Can you give at least one argument in defense of the second option? Here is the answer.

V
vilinyh, 2021-06-30
@vilinyh

Blocks of control structures are easily folded and unfolded with hotkeys. In addition to this, paired curly braces are highlighted by the editor.
But the alternative syntax highlighting still does not work in PhpStorm, and you have to look for paired if-endifs with your eyes.
60dcbcb8ae841689225493.png
Well, and, for fun, try using an alternative syntax for switch - this is a carefully charged rake.

A
Alexander, 2021-06-30
@poter

It's even written in the documentation. What is recommended is the first way in templates.
When you have a bunch of code and bam } occurs, you will understand every second that this is easily lost from sight.
endif etc. much clearer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question