What is the difference between foreach uses in php?

5

What is the difference between these two means of using foreach in php? Would one be more special than the other, bringing some advantage for example?

Method 1:

foreach ($arr as $key => $value) {
    #do something 
}

Method 2:

foreach ($arr as $key => $value): 
  #do something 
endforeach;

Are there other variations like this for foreach or for other similar instructions in php?

    
asked by anonymous 03.11.2016 / 02:34

3 answers

2

Complementing the existing answers and avoiding to speak more of the same, within this context also enters the code standards.

There are standards widely accepted by PHP communities around the world.
* It does not mean that everyone accepts. Some refute or ignore it.

An example is link patterns

We can mention the PSR-2.

Everything that is "alternative syntax" or "aliases" always gets out of code patterns. In the case of PSR-2, the standard always proposes to use%% keys for opening and closing of flow control structures and declaration of functions and classes.

Example

class Foo
{

}

There are certain detailed rules, for example, this is considered "wrong"

class Foo 
{

}

Visually it looks the same as the first, but note that there is a space before breaking the opening of {} . IDEs in modal mode may issue a correction notice.

For PHP, it's indifferent:

class Foo {

}

class Foo{}

The issue is more visual and optimizing for IDEs and third-party compilers and, of course, writing organization / standardization.

There was a time that many omitted the chronchetes in 1-line instructions and even was widely spread and became standard in many documentations:

if(instrução)
    //faz isso

//outro código

Another way

if(instrução)
    //faz isso
else
    //faz aquilo

Currently the recommendation is to avoid suppressing the brackets as this can bring confusion. In PSR-2, ask that even when there is only 1 line, always write the bracket.

if (instrução) {
    //faz isso
} else {
    //faz aquilo
}

Note also that there are details in this last example:
- Soon after the if there is a space.
- There are no spaces in parentheses.
- Just after closing the parenthesis there is a space

It is important to maintain this standard because many platforms adopt the PSR-2 and validate codes within these standards. You may have trouble having to adapt hundreds of thousands of codes by following another unknown pattern or inventing one that is not equal to the PSR.

There are certain tolerances for alternative syntax such as the use of this syntax

echo (condição)? 'ok': 'ng';

Here I mentioned if, but the same applies to other flow control structures

foreach () {

}

while () {

}

for () {

}

Note that the above examples are based on the PSR rules. Code pattern standards may vary depending on which platform you use.

One example, Wordpress allows something like {

This space in the condition does not allow other platforms that do not tolerate it, such as the PrestaShop syntax validator, for example.

But Wordpress lets you spell if ( condition ) { .

Therefore, it is always better to follow what is most widely accepted because a code you write for a platform can be more easily accepted on a third platform.

An example using PrestaShop itself, this platform, up to 2 years ago, required deleting keys in one-line statements:

if ()
    //uma linha

However, they recently changed that pattern. Currently not allowed. Those who used the PSR-2 standard did well because they had no trouble changing anything. PrestaShop has adopted the PSR-2 and enforces its use.

The same can happen with other platforms like Wordpress, Magento, etc.

* I've cited these platforms as examples for being popular.

There are still those who refuse to use the php-fig.org defaults. As for this we can not do anything because this site is completely independent of PHP and is not a kind of law where everyone is required to follow.

However, one fact that nobody can deny is that these PHP-fig standards are widely accepted worldwide. You can be in Afghanistan, Mexico, Brazil, Vietnam or Greenland and you'll find PHP programmers following this pattern. It is not something localized where people from one country follow and the rest of the world is unaware.

Here I will avoid commenting on indentation and line break patterns in long conditionals because the focus of the question is specific to using alternate syntax on if (condition) { .

    
03.11.2016 / 07:31
7

I've seen this before, but I just checked. There's no difference, technically speaking.

The second method is most commonly used when there are html codes in the middle, for example:

<?php
    $arr = blah();
    foreach ($arr as $key => $value):
?>
<h1>Hello .. <?php echo $value; ?></h1>
mais código html aqui...
<?php endforeach; ?>

It looks more elegant than this:

<?php
    $arr = blah();
    foreach ($arr as $key => $value) {
?>
<h1>Hello .. <?php echo $value; ?></h1>
mais código html aqui...
<?php } ?>

There are several others. if/endif ; switch/endswitch ; for/endfor . You can check the php manual

    
03.11.2016 / 02:52
7

There is no difference in terms of operation, internally both will become the same when executed.

Its raison d'être is merely a choice, it seems, for better readability in different contexts. There are situations where the indentation gets confusing, especially when mixed with HTML blocks, as already said by colleague @ Kirito94, but this can only be analyzed case by case.

As the PHP manual says:

  

PHP provides an alternative syntax for some control structures; namely, if, while, for, foreach, and switch. In each case, basically the alternative syntax is to change the colon key (:) and the closing key to endif ;, endwhile ;, endfor ;, endforeach ;, or endswitch ;, respectively.

Also, in the manual, it is clear that it works in the blocks of else and elseif

<?php
if ($a == 5):
    echo "a equals 5";
    echo "...";
elseif ($a == 6):
    echo "a equals 6";
    echo "!!!";
else:
    echo "a is neither 5 nor 6";
endif;
?>

More details on the link:

Alternative Syntax for Control Structures .

    
03.11.2016 / 02:54