Do not use "{}" keys in IF in PHP can generate problems?

15

Generally for checks that modify only one line I have avoided the use of keys, since 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 these cases? is there the possibility of php returning error or something of the sort?

    
asked by anonymous 11.07.2014 / 19:34

6 answers

20

Actually IF no keys can only be used in executing a command and when it is a command string you use the keys to indicate that, at time { to term } execute. See the examples below.

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 even 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'm going to leave a demo of for and while in action. DEMO

    
11.07.2014 / 19:45
15

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

Many people always recommend using the keys to avoid problems during code maintenance. If you omit the keys and at some other time someone (even you) decides to include a second statement in the body of if or else , you can forget to add the 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 may go unnoticed:

if($foo == 2) 
   echo 'aaaa';
else
   echo 'bbbb';
   echo 'cccc'; // executa incondicionalmente!!!
    
11.07.2014 / 20:00
4

While running, ( IF no keys can only be used when executing a command ) you will have a writing and organizing problem, so it is recommended that you follow what was 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 and so on   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 is used instead of else if so that all   control keywords look like single words.

    
11.07.2014 / 19:44
4

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

But if you do not like the keys, you have another option:

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

The same as while, for and foreach, but the end is changed to its name with "end" previously.

Example

    
12.07.2014 / 07:13
0

When you omit the keys you will only treat the next statement 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 print "bar"

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

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

    
11.07.2014 / 19:49
-2

Colleagues on the support I have already encountered errors due to missing quotes in if

code .....

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

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

    
17.11.2016 / 13:33