Fact is it's personal preference.
I personally don't use any tabs - mostly because it will then depend on the editor as to how the code will be displayed.
Some of my personal preference for coding includes:
- 2 spaces to indent
- No space before ( or after )
- 1 space before {
- Don't waste a line for {
- variables/functions don't have capital letters
But every one of those can NOT be done and the code in PHP will still work. A lot of the personal preference is derived off what you learned in, what you're used to doing, what you're used tor reading, etc.
For example, a function I wrote would look something like this:
function myfunction( $var ){
if( $var == "a" ){
print( "You passed a.<br>\n" );
return( "1" );
} else {
print( "You passed something other than a.<br>\n" );
return( "0" );
}
}
But the same function COULD look like this:
function myfunction ($var)
{
if ($var == "a"){
print ("You passed a.<br>\n");
return ("1");
} else {
print ("You passed something other than a.<br>\n");
return ("0");
}
}
or
function myfunction ($var)
{
if ($var == "a"){ print ("You passed a.<br>\n"); return ("1"); }
else { print ("You passed something other than a.<br>\n"); return ("0");}
}
They would all probably work the same. Just pick a method YOU like and stick to it.
How your code looks isn't as important as how well you document it 😉
As for when to use a function - that really requires some planning on your part. If you think you'll need to re-use some code, find a way to turn it into a function. You obviously COULD write a function for one-time usage, but I generally only do that if I'm trying to keep the code within one script to a minimum. Using include() and require() can really help you out too when it comes to re-using code. You should never say "oh I need to copy & paste the code from this script into this other script I'm using in the same project" - use a function or include() another php file!