PHP, JS, CSS Coding Standards

17 Jan 2017

Filenames

PHP

Coding styles

Follow PSR-1 and PSR-2 Coding Standards.

Filenames

Class names

Class method names

Global function names

Method visibility

public function getValue()
{
    // ...
}

Variable names

<?php
$userName = 'admin';
$count = 100;
$amount = 123.45;
$i = 0;
$status = true;
$rows = array();
$customer = new Customer();
$db = $this->getDb();

Special variable names

<?php
$result            // Mixed return value
  
// Parts of file name and path
$splFile = new \SplFileInfo('/tmp/file.txt');

// Check if file or path exists
$existsPath = $splFile->getRealPath() !== false;

// Full path, filename and extension (e.g. /tmp/file.txt)
$file = $splFile->getRealPath() ?: $splFile->getPathname();

// Filename with extension (e.g. file.txt), or the last directory name
$fileName = $splFile->getFilename();

// Filename extension without dot (e.g. txt)
$extension = $splFile->getExtension();

// Filename without extension (e.g. file) or the last directory name
$baseFileName = pathinfo($splFile->getFilename(), PATHINFO_FILENAME);

// Full path to file (/tmp)
$filePath = pathinfo($splFile->getRealPath() ?: $splFile->getPathname(), PATHINFO_DIRNAME);

// Name of a single folder (e.g. tmp)
$directory = 'tmp';

Array index names

Comments

HTML

Filenames

JavaScript

Filenames

Documentation

Variables

const userName = 'admin';
const total = 100;
const amount = 29.99;
const visible = true;
const config = {}
const rows = [];
const value = 'something';

Element names

// good
let row = {};
row.id = 100;
row.username = 'admin';

// good
const row = {
    id: 100,
    username: 'admin'
};

// bad
var row = [];
row.Id = 100;
row.UserName = 'admin';

CSS

Filenames

Names

.my-super-class-name {}
#email
#field-with-long-id
#my-modal
#anchor-name

Vue.js