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) {
.