Avatar of Crazy Horse
Crazy Horse
Flag for South Africa

asked on 

To use { or not to use { - that is the question

Since I started learning php recently, I always use { after IF. For example:

if ($_SERVER['REQUEST_METHOD'] == "POST") {
            echo "okay";
      }

I was watching a tutorial the other day and noticed that the person did not use those braces e.g.:

if ($_SERVER['REQUEST_METHOD'] == "POST")
            echo "okay";
      
I tried this myself and the code still worked. I am confused now as to whether I need the braces or not if I don't have an else to add afterwards. I would rather use the braces always so I don't forget about them but just wondered if it is okay to do so even if there isn't an ELSE accompanying the IF.
PHP

Avatar of undefined
Last Comment
gr8gonzo

8/22/2022 - Mon