vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 3 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=187)
-   -   Coding Syntax (https://vborg.vbsupport.ru/showthread.php?t=42946)

Scott MacVicar 08-29-2002 10:00 PM

Coding Syntax
 
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.

g-force2k2 08-31-2002 03:57 PM

wow PPN ;) thanks alot :) now i can see some light as to why vb3 will be in fact diferent ;) thanks for sharing the very useful information... regards...

g-force2k2

Scott MacVicar 08-31-2002 03:59 PM

Everything is more or less coded to a standard, anything thats not will get changed eventually.

g-force2k2 08-31-2002 04:03 PM

i see around all the array stuff

ie ::

PHP Code:

$bbuserinfo['usergroupid'

instead of the usual ::

PHP Code:

$bbuserinfo[usergroupid

i've read that the first one (that vb3 uses) is the correct way to do it then the second one... can you confirm that this is in fact true?

Also should we not use the second example anymore? regards...

g-force2k2

Scott MacVicar 08-31-2002 04:09 PM

Yep vB3 uses the first and it is correct, if you dont have any quotes then its an undefined constant, using the quotes indicates a string.

Added a bit about using 'and' and 'or' instead of || and &&

g-force2k2 09-01-2002 06:28 PM

question PPN?

whats the difference btw using? the positioning of if?

ie ::

PHP Code:

if ($_REQUEST['action'] == "go_here") { 

and

PHP Code:

if($_REQUEST['action'] == "go_here") { 

does that space make it more efficient or something?

And is this $_REQUEST and $_POST vars vb3 creations or are they standard with php? regards...

g-force2k2

Scott MacVicar 09-01-2002 09:42 PM

Em just cosmetic with the if, it looks nicer ;)

its the PHP Super Globals http://www.php.net/manual/en/languag...predefined.php for $_POST and $_REQUEST.

Though $_REQUEST is used because its all input from the user.

Admin 09-03-2002 12:17 PM

There are 7 super global arrays in PHP, as of 4.1.0:
$_GET - contains all information that passed in the address bar (or a form with get method)... used to be $HTTP_GET_VARS.
$_POST - contains all information that is passed through a posted form (if the form was using the post method)... used to be $HTTP_POST_VARS.
$_COOKIE - contains all cookie information... used to be $HTTP_COOKIE_VARS.
$_REQUEST - simply a merge of all three arrays above... new.
$_FILES - contains information about uploaded files... used to be $HTTP_POST_FILES.
$_ENV - all environment variables... new AFAIK.
$_SERVER - all server variables... used to be $HTTP_SERVER_VARS.

Note that in vB3 these arrays are NOT super globals, or at least you must not assume they are. You'll see why when you actually see the code in init.php.

DraX 09-28-2002 04:55 AM

This brings up a question... will vB3 still be compatible on servers running older versions of php?

Due to an operating system conflict (damned Sun Microsystems machines), I can't safely upgrade my version of php without the potential of breaking the control panel for the OS....

Is there a function to determine if the version of php supports the super globals, such as $_REQUEST and if not, to define it as a new global using $HTTP_GET_VARS['REQUEST'] (I think that was right... off the top of my head)?

JulianD 09-28-2002 06:19 AM

Yeah, it's one of my main concerns about vb3. What will be the minimun required PHP version to run vb3?

Scott MacVicar 09-28-2002 09:29 AM

PHP 4.0.6 was set at the minimuim as its the latest secure version you could have, as php.net provides a patch for php 4.0.6 to patch the upload security holes.

TECK 09-28-2002 11:53 AM

Quote:

Originally posted by PPN
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';
    }



Scott, can you please explain to me why this approach? i think this will only slow down the code writing process.
first, it takes alot longer to code using this technique.
second, the file will be huge in leght.
third, is a waist of time for every time you press an extra Enter.

i will not upgrade anyway to VB3, for a while. not until 3.1 or 3.2 is released.

Scott MacVicar 09-29-2002 01:32 PM

It was done so that the coding is more spaced out so you can see and understand the code more clearly. It also is better for editors which support brace matching like Komodo and TextPad

TECK 09-29-2002 01:33 PM

true... i use textpad and the ctrl+m is a great feature...

JulianD 10-07-2002 03:13 AM

Quote:

Originally posted by PPN
PHP 4.0.6 was set at the minimuim as its the latest secure version you could have, as php.net provides a patch for php 4.0.6 to patch the upload security holes.
Uhmmm Does 4.06 supports these kinds of arrays? $_REQUEST, $_GET, $_POST, etc?

Scott MacVicar 10-07-2002 06:17 AM

no but the way it works vbulletin 3 will create these arrays.


All times are GMT. The time now is 07:38 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01338 seconds
  • Memory Usage 1,769KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (9)bbcode_php_printable
  • (2)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (16)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete