Can not use "{ }" IF keys in PHP cause problems?

Asked

Viewed 2,856 times

19

Usually for checks that modify only one line I have avoided the use of keys, because PHP accepts this method, but I rarely see codes from other programmers like this... Examples

My method

if($thumb->SaveInDB('imagens','img'))
    $ok = true;
else
    $ok = false;

Normal method

if($thumb->SaveInDB('imagens','img'))
{
    $ok = true;
}
else
{
    $ok = false;
}

Does it make any difference to use the "{ }" keys in such cases? is there the possibility of php returning error or something like?

  • It still works. I use keys for organization.

  • The answers from @Diegovieira, bfavaretto and gpupo complement each other well... I think it would be nice if someone included in their reply references the others to make the answer well complete :D

  • related: https://answall.com/questions/162974/qual-a-diferen%C3%A7a-entre-os-usos-do-foreach-no-php

6 answers

23


In fact the IF without keys can only be used when executing a command and when it is a chain of commands you use the keys to indicate that at the instant { until the end } execute. See the examples below.

The same logic applies to languages such as PHP, Javascript, Typescript and C#.

This will work!

PHP

if($meuVar == true)
   echo 'verdadeiro';
else
   echo 'falso';

if($meuVar == true)
{
   echo "Sim";
   echo "<br> É verdadeiro";
}
else
   echo "Não <br> É verdadeiro";

This will NOT work!

PHP

if($meuVar == true)
   echo 'Sim';
   echo '<br> é Verdadeiro';
else
   echo 'Não';
   echo '<br> é Verdadeiro';

I myself use in my codes when I will execute only one command, this method also works in other languages such as C#

This also applies to other commands, examples below.

PHP

for($i == 0; $i < 10; $i++)
   echo $i;

while($i < 10)
   echo $i++;

foreach($items as $item)
   echo $item->meuCampo;

I’ll leave a demo of for and the while in action. DEMO

  • So in fact the { } only serve to ensure that we have a chain of executions, in other words there is no possibility of errors when there is no chain... would be more or less that?

  • if the command is used correctly it will not show errors, but remember, only in the execution of a command, in the @bfavaretto example it shows that logic error may occur.

17

This is a controversial debate in several languages, not just PHP. There is no correct answer. Ultimately it is a matter of style and will not generate problems if all necessary care is taken.

Many people always recommend using the keys to avoid problems during code maintenance. If you omit the keys and at another time someone (even you) decides to include a second instruction in the body of if or of else, can forget to add keys, and generate a syntax error:

if($foo == 1) 
   echo 'aaaa';
   echo 'bbbb'; // erro de sintaxe
else
   echo 'cccc';

Or, even worse, a logic error that can go unnoticed:

if($foo == 2) 
   echo 'aaaa';
else
   echo 'bbbb';
   echo 'cccc'; // executa incondicionalmente!!!

6

As all answers said, you can only use ONE command after an if without keys

But if you don’t like the keys, you have another option:

if($algumacoisa) :
    echo 'foo';
    echo 'bar';
endif;

The same for while, for and foreach, only the ending is changed to the respective name with "end" previously.

Example

5

Despite executing,(IF without keys can only be used for executing a command) you will have a writing and organization problem, so it is recommended that you follow what has been decided on PSR-2:

5.1. if, elseif, Else

An if Structure looks like the following. Note the placement of parentheses, Spaces, and braces; and that Else and elseif are on the same line as the closing Brace from the earlier body.

<?php
if ($expr1) {
    // if body
} elseif ($expr2) {
    // elseif body
} else {
    // else body;
}

The keyword elseif SHOULD be used Instead of Else if so that all control Keywords look like single words.

  • Observation. Trocaria o que foi decidido na PSR-2 for a recomendação do PSR-2

1

When you omit the keys you will only treat the next instruction as the condition body:

if ($x) echo 'foo'; 
é o mesmo que 

if ($x) {echo 'foo'; } 
mas lembre-se que 

if ($x) 
   echo 'foo'; 
   echo 'bar'; 

always prints "bar"

PHP treats everything in {} as a single expression "grouped".

The same goes for the other control instructions (foreach and so on)

-2

Colleagues support already encountered errors due to missing quotes in if

code.....

if(true)
    function x();
    function Y(); 

executes function y() as part of the code without taking into account the if that error in a code not very readable made me spend a day and a half and causes random errors hard to find

Browser other questions tagged

You are not signed in. Login or sign up in order to post.