Indenting code helps to keep statements in logical groups, making it easier to understand the flow of the script. There are no fixed rules; PHP ignores any whitespace inside code, so you can adopt any style you like. The important thing is to be consistent so that you can spot anything that looks out of place.
The limited width of the printed page means that I normally use just two spaces to indent code in this book, but most people find that tabbing four or five spaces makes for the most readable code. Perhaps the biggest difference in styles lies in the way individual developers arrange curly braces. I align the closing brace with the block of code it concludes. Other writers use this style:
Yet others use this style:
Choose whichever style you’re most comfortable with. As long as it’s consistent and easy to read, that’s all that matters.
The limited width of the printed page means that I normally use just two spaces to indent code in this book, but most people find that tabbing four or five spaces makes for the most readable code. Perhaps the biggest difference in styles lies in the way individual developers arrange curly braces. I align the closing brace with the block of code it concludes. Other writers use this style:
if ($bytes > 51200) {
// display error message and abandon upload
} else {
// continue upload
}
// display error message and abandon upload
} else {
// continue upload
}
if ($bytes > 51200)
{
// display error message and abandon upload
}
else
{
// continue upload
}
{
// display error message and abandon upload
}
else
{
// continue upload
}
Choose whichever style you’re most comfortable with. As long as it’s consistent and easy to read, that’s all that matters.