<?php
// Did you notice the full-style opening PHP tags?
/**************************************************************
* Variable Naming conventions
*************************************************************
* - Use data type as prefix inside the name
* Eg: $sName instead of just $name
* - Try to use as descriptive names as possible
* Eg: $arProductList instead of just $arList
**************************************************************/
/**************************************************************
* Basic Datatypes conventions
**************************************************************/
// Strings
$sName = "tekkie.ro";
// Integers
$iYear = 2004;
// Floats
$fAccountValue = 12.34;
// Booleans
$bCheck = true;
// Objects
$oProduct = new Product( $iProductId );
// Arrays
$aProperties = array ('1', '2', '3');
/**************************************************************
* Spacing convention
**************************************************************/
$iSum = $iNumber1 + $iNumber2;
$sSum = $iNumber1 . $iNumber2; // space dot space before and after a variable
$sSum = ' Here we add a number ' . $iNumber1; // quote space dot space after a string
$sSum = $iNumber2 . ' Here we add a number '; // space dot space quote before a string
/*
* Function definition convention
*
* A comment describing the function should precede its declaration.
* Begin with a slash followed by two stars.
* Give a one-line function synopsis, then a brief explanation.
*/
/**
* bool hasPrices (obj oProduct [, bool bAlwaysTrue])
*
* @param Product the product we chack for prices
* @param boolean
* @return true if the Product has prices, false otherwise
*/
function hasPrices( $oProduct, $bAlwaysTrue = false ) {
if ($bAlwaysTrue) return true;
foreach ($oProduct->aProperties as $sProperty ) {
if( 'price' === $sProperty ) { // use === as often as possible; put the string first for speed reasons
return( true );
} // .. if
}
return( false );
} // END func hasPrices()
/*
* echo tag
*/
define (CR, "\n");
echo
CR, 'Line1<br />',
CR, 'Line2<br />',
CR, 'Line3<br />'
;
/**************************************************************
* Spacing convention
**************************************************************/
// Code like this:
$sText = 'Time flies like an arrow.';
$sText .= ' Fruit flies like bananas.';
// Should be done in one statement instead of two:
$sText =
'Time flies like an arrow.' .
' Fruit flies like bananas.'
;
?>
I’d love reviewing your code, great standards.
One thing I’ll never get is adding the
// END
comment after said structure. I just use brace matching for that.
I work on a laptop, so that scenario happens quite frequently. I agree that having a comment by the end of the brace helps a lot, but I just use the ‘jump to matching brace’ which also shows the function’s signature or the condition I was testing.