Jump to content

Closing/opening Brackets


jimfog

Recommended Posts

Look at the following code:

if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");

According to the author if the month is not "requested"(not passed to the request superglobal array by a user input) it will default to date("n")-the "request" array. My q is the following, since this is an if statement shouldn't this piece of code:

$_REQUEST["month"] = date("n");

be enclosed in brackets{}? Thanks.

Link to comment
Share on other sites

it doesn't need curly braces because it is only one statement.

if (!isset($_REQUEST["month"]))  $_REQUEST["month"] = date("n");

if you added a second statement then you would get an error and would need to enlose them all with the curly braces.

Link to comment
Share on other sites

It wouldn't be an error, it would only include the first line in the if statement and the second line would always be executed. If you have any control structure, like a loop, conditional, etc, and the body contains multiple lines then you need the brackets. If you leave out the brackets then it assumes that the body only contains one line.

Link to comment
Share on other sites

Thanks for the valuable info-i really did not know that.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...