_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.
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.
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.
Used for CSS styles.
SQL column names will use WordCaps. For example: FirstName, LastName, etc.
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.
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
}
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.
Code should line up visually. This is true for PHP, Javascript, SQL and any other language you may work on.
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.
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;
For CSS styles we are using kebab-case.
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:
Within each of those sections, alphabetize them.
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 {...}
For readability we will have one space between any two separate items as defined here:
For example:
.page-list .content {
box-shadow: var(--box-shadow);
background-color: var(--secondary-bg-color) !important;
color: #888;
}
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;
}
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 that signifies a Yes or No will be defined as:
`ColumnName` ENUM('Y','N') DEFAULT 'N'
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`.
For address-related SQL columns use `City` instead of `Town`
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.
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.
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.
SELECT fncOnlyDigits('(209)585-9075cell') AS 'PhoneReadyForSMS';
SELECT fncFormatPhone(123456789,'###-##-####');
Result: 123-45-6789
SELECT fncFormatPhone(123456789,'(###) ###-####');
Result: (012) 345-6789
SELECT fncFormatPhone(123456789,'###-#!##@(###)');
Result: 123-4!56@(789)
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.
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!