Back to vBulletin 3 Articles

Coding Syntax
by Scott MacVicar 30 Aug 2002

Due to the release of vB3 being around the corner (i like being vague ) I just thought I'd post some coding standards that

have been introduced.

Variables
They must have appropriate names to the data they contain if you've just selected data from the user table don't call it

$stuff or anything like that.
When your reffering to any information passed in by the user use $_REQUEST['variablename'] or $_POST['variablename'] instead

of $variablename, some nice register_globals off compliance.

Basic Syntax
Use tabs to indent the line, this should be done everytime you open a new set of parenthesis and will continue until the closing parenthesis.

Parenthesis should be on a new line of there own like below

PHP Code:
if ($_REQUEST['do'] == 'var')
{
    
$var 'test';
    if (
$bbuserinfo['usergroupid'] == 6)
    {
        echo 
'your an admin';
    }

If you are using if or elseif, make sure there is a space between that and the comparison.

Use singlequotes from now on where possible, you will not be able to do this when you have a variable in the string as this

will not be evaluated with the string. This includes keys in arrays, unless the key is another array you would use the second example in the following code.

PHP Code:
if (empty($array["var"]))
{
    echo 
'blah';
}

//CORRECT USAGE

if (empty($array['var']))
{
    echo 
'blah';
}

//if you are referring to the key by another array use
if (empty($array["$array2[var]"]))
{
    echo 
'blah';

Functions
When reffered to there should be no spaces between the name and the parameters being passed in.

PHP Code:
imaginary_function($var$rank); 
When passing in parameters to a function ensure that you have spaces between the comma and each variable being passed in.

Depreciated Functions
It was common to see the following in vBulletin 2, the second version is what will be used in vBulletin 3.

PHP Code:
if (isset($action) == 0)
{
    
$action 'modify';
}

//USE THIS INSTEAD

if (empty($_REQUEST['do']))
{
    
$_REQUEST['do'] = 'modify';

Expressions
With an if expression where you are comparing two or more items use OR and AND, not || and &&. This is simply due to the fact that its easier for the human to read.

I know i've forgotten stuff and I will add more as I can think of it.

Hopefully this will improve coding in general when your not using vBulletin and writing your own scripts.

vblts.ru supports vBulletin®, 2022-2024