When coding for clients in their environment we of course use their coding standards. When coding on new projects or if clients do not have coding standards, then we use the below Programming Labs coding standards.


Case Types

  • _UPPERCASE

    PHP Constants will all start with an underscore then have text be in all CAPS.

    Note: if a variable in PHP site has a set value and never changes, it should be changed into a constant.

  • kebab-case

    Used for CSS styles.

  • WordCaps

    SQL column names will use WordCaps. For example: FirstName, LastName, etc.

  • camelCase

    Used for everything else.
    PHP variable names, PHP and JS function names, and SQL table names. Also used for PHP, CSS, and JS file names.


Coding Standards

  • Variable Names and Scope

    We prefix each variable based on what scope it is in both PHP and JavaScript.

    If the variable is used in more than one page/file then it is considered a global variable. For example in PHP pages if a variable is defined in an "include" file and referenced in another file, that is a global variable.


    Scope Prefix Examples
    function fnc $fncSQL, $fncMenu
    page pg $pgSQL, $pgHtm
    global glo $gloConnected, $gloPDOrow

    Temporary variables that are only used in a short loop can be single character since they will never be searched on. For example:


    for ($i = 0; $i < $numfields; $i++) {
        // do something
    }

  • Indentation and Alignment

    Code should have the beginning statement and end statement line up with eachother.

    Code in-between the beginning and end statement should be indented one-tab. Each tab should be 4 spaces in length.

  • Spacing

    Code should line up visually. This is true for PHP, Javascript, SQL and any other language you may work on.

    1. Spaces should be used liberally to make sure equal signs line up if there is a series of values being set.
    2. Spaces should be used around variables and periods to make lines easier to read.
    3. In general, if a space does not cause a problem it should be inserted so developers can use the Control key with the Arrow keys to navigate. If spaces are skipped then that makes keyboard navigation not work for some text editors... as well as making the code difficult to read.

    When looking at your code and trying to decide if it looks like the spacing is correct assume your project manager will be called at 2am after only 3 hours of sleep and has to read your code with blurry eyes. If you cannot see the general structure of your code with blurry eyes then you do not have your spacing correct.

  • PHP Syntax

    As a company standard we do not use curly bracket syntax. We use the alternative like:


    if ($pgIPaddress != 'no-IP'):
        $pgMsg = "Your IP address ($pgIPaddress) has been logged.";
    else:
        $pgMsg = 'IP address not recognized.';
    endif;

  • CSS Standards

    For CSS styles we are using kebab-case.


    Order of Items

    So we can easily read and find things in the CSS files, for files we create we will put the styles in alphabetic order using the following rules:

    1. core elements defined at top (body, h1, h2, etc.)
    2. styles listed next (.btn syntax)
    3. id defined styles listed next (#some-id syntax)
    4. @media only screen and (max-width: ...)

    Within each of those sections, alphabetize them.


    Special Situations

    If there is a combination of different types then place based on the highest in the hierarchy. For example:

    h1 { ... }
    h2 .big-text #otherHeader {
    ...
    }
    h3 {...}


    Spacing

    For readability we will have one space between any two separate items as defined here:

    1. One space between styles on the same line.
    2. One space after last style on the line, then the open bracket.
    3. One space after colons.
    4. One tab before each style item. Each tab shall be four spaces in length.
    5. One space before !important or other secondary definitions.

    For example:

    .page-list .content {
        box-shadow: var(--box-shadow);
        background-color: var(--secondary-bg-color) !important;
        color: #888;
    }


    Minimized Definitions

    The only time a combinator should be used is if the style should only work within that specific element. For example, the #myBtn1 only exists in one place so it should be defined as:

    #myBtn1,#myBtn2,#myBtn3,#myBtn4 {
        font-weight: bold;
    }

    ... not as:
    .features #myBtn1,#myBtn2,#myBtn3,#myBtn4 {
        font-weight: bold;
    }


SQL Data Standards

  • SQL Table Standards

    The first two columns for all SQL tables shall start with:

    `UID` int UNSIGNED NOT NULL auto_increment,
    `AddDate` timestamp NOT NULL default CURRENT_TIMESTAMP,

  • Data Flags

    Data that signifies a Yes or No will be defined as:

    `ColumnName` ENUM('Y','N') DEFAULT 'N'

  • TEXT Column Naming

    As with all column naming, using descriptive names makes them easier to search for. As a standard, names that end in `Note` should be singular `Note`, not `Notes`. For example `InternalNote`, not `InternalNotes`.

  • City vs Town

    For address-related SQL columns use `City` instead of `Town`

  • Zipcode

    SQL column names which are zip codes for addresses shall be defined as:

    `Zipcode` varchar(10)

    This works best for USA and Candada zip codes and prevents problems regarding zip codes that start with '0' needing to use LPAD.

  • Many to Many Tables

    Naming convention for many-to-many SQL tables will be:

    DominantTable_X_SecondaryTable

    This way it will show up alphabetically in the SQL table list next to the dominant table and the _X_ will make it obvious this is a cross-reference table used to connect two tables.

  • Phone Numbers

    SQL column names which are phone numbers shall be defined as varchar(20) like:

    `CellPhone` varchar(20)

    Our library has two SQL functions to allow manipulation of phone numbers or any numbers for that matter.


    fncOnlyDigits     example usage:

    SELECT fncOnlyDigits('(209)585-9075cell') AS 'PhoneReadyForSMS';


    fncFormatPhone     example usage:

    SELECT fncFormatPhone(123456789,'###-##-####');
    Result: 123-45-6789

    SELECT fncFormatPhone(123456789,'(###) ###-####');
    Result: (012) 345-6789

    SELECT fncFormatPhone(123456789,'###-#!##@(###)');
    Result: 123-4!56@(789)


Wizard’s Toolkit

  • Low-Code Library WizardsToolkit.com

    For this low-code library, all PHP functions shall start with 'wtk'. This way they can easily integrate with other libraries without fear of function names being duplicated.

    Likewise all SQL tables specific to Wizard’s Toolkit start with 'wtk'.

    All Wizard’s Toolkit CSS and JS external file names also start with 'wtk'.

Good coding standards makes it so you can find code faster, read and understand it easily, and increase reusability. One hour of forethought and planning can prevent literally hundreds of hours of unnecessary upkeep, straining for interpretation, and possible future bugs.

  1. Readability
  2. Text Searching
  3. Consistency
  4. Reusability

On average programmers spend 10 hours reading code for every 1 hour of actual coding. If Coding Standards are used properly this can be reduced significantly. Imagine having to work 11 hours and knowing 10 of those hours will be reading and deciphering code so you can do one hour of coding and inserting it into the proper locations. If Coding Standards and Naming Conventions are strictly adhered to it is completely possible to change that so in an 8 hour day you spend only 4 hours reading code and the other 4 hours actually coding. Work 3 hours less and be 4 times more productive!