title: PHP - If/else, for, foreach, while - without curly braces?
body: <p>Something that really would like to know but never found out are shortcuts in PHP.</p>
  
  <p>I am currently coding a function with a <code>foreach</code> loop with just a single statement inside. I tried to omit the curly braces as you can do in if/else control structures and it works. No errors.</p>
  
  <pre><code>foreach($var as $value)
      $arr[] = $value;
  </code></pre>
  
  <p>Now I tried to use it the same way but putting an if/else block inside it. Again, working and no errors.</p>
  
  <pre><code>foreach($var as $value)
      if(1 + 1 == 2) {
          $arr[] = $value;
      };
  </code></pre>
  
  <p>Then, I thought like "why is this working?" and omitted the closing semicolon. Still working. So I tried to use the if/else statement without curly braces inside the foreach loop and again, still working and no errors. But is the foreach loop really closed/ended right now?</p>
  
  <pre><code>foreach($var as $value)
      if(1 + 1 == 2)
          $arr[] = $value;
  </code></pre>
  
  <p>At least I omitted the closing semicolon again and (as expected) a parsing error occurred.</p>
  
  <p>So my big question is: When can I omit the curly braces and in which structure/loop/function? I know that I can definitely do so in <code>if</code> and <code>else</code>. But what about <code>while</code>, <code>for</code> and <code>foreach</code>?</p>
  
  <p>And yes, I know that it is not safe, smart, whatever to code without curly braces and there are shorthands like <code>$condition ? true : false;</code> and <code>if: doSomething(); endif;</code>, <code>endfor;</code> and <code>endforeach;</code>. I don't wanna learn about shorthands I just want to understand the conditions about when and where it is possible to omit the curly brackets.</p>