What happens if I do not specify {}?

6

I have this code that works normally:

if($time <= time()) {       
     if($time != 0)
         $Server->wsSend(1, 'perfect world');

        foreach ( $Server->wsClients as $id => $client ) 
            $time = time() + $adminMessage['interval'];
}

I can not change it because it does not specify the {} of if nor foreach . If I put { } on foreach wsSend no longer works.

Why is it working?

    
asked by anonymous 15.03.2015 / 16:52

2 answers

10

The reason for the problem exists in PHP

As C has become an extra popular language, to succeed others seek to copy their syntax, as is the case with PHP.

The problem

This is called dangling else . Technically you are not using else but the problem is essentially the same. This is a problem created by the way the C language has decided the syntax of if , where you can have either a single logical line of execution or a block of execution.

If you need to execute only one line, simply place this line immediately after the condition. It can be placed on the same physical line as if or later, which will determine what has ended is the semicolon.

If you need an execution block, you need to explicitly delimit them through the keys. This way you can put how many lines (that is, how many semicolons) you want inside the block, which will determine your end is the "key closure."

Although it seems a good idea to save typing and make the code slightly more concise when you have only one line, it is very common to get lost in control of this and end up creating unwanted logic. This occurs when the programmer plans to have a line and ends up needing to put more than one. Then he forgets that the keys become mandatory.

Indent does not count anything to determine what will be executed, it is only for the programmer to better visualize what he is doing. So your code in the background should be indented like this (which would probably make you realize the error):

if($time <= time()) {       
     if($time != 0)
         $Server->wsSend(1, 'perfect world');
     foreach ( $Server->wsClients as $id => $client ) 
         $time = time() + $adminMessage['interval'];
}

Or to visualize better:

if($time <= time()) {       
     if($time != 0) {
         $Server->wsSend(1, 'perfect world');
     }
     foreach ( $Server->wsClients as $id => $client ) {
         $time = time() + $adminMessage['interval'];
     }
}

So clearly foreach is not within if . If your intention was that it was, the only solution would be:

if($time <= time()) {       
     if($time != 0) {
         $Server->wsSend(1, 'perfect world');
         foreach ( $Server->wsClients as $id => $client ) 
             $time = time() + $adminMessage['interval'];
     }
}

I would do even better:

if($time <= time()) {       
     if($time != 0) {
         $Server->wsSend(1, 'perfect world');
         foreach ( $Server->wsClients as $id => $client ) {
             $time = time() + $adminMessage['interval'];
         }
     }
}

The solution

Just because it may, does not mean that you should avoid the keys when you have only one line. Keeping the standard of always using the keys, regardless of whether it is necessary or not, you avoid these unwanted errors.

    
15.03.2015 / 17:36
4

Each if or foreach that does not come with the keys ( { and } ) will take into account only the next command.

So the code you posted equals the code snippet below:

if ($time <= time()) {       
    if ($time != 0) {
        $Server->wsSend(1, 'perfect world');
    }
    foreach ($Server->wsClients as $id => $client) {
        $time = time() + $adminMessage['interval'];
    }
}
    
15.03.2015 / 17:03