Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

Function names should suggest an action or verb. Use names like updateAddress or
makeStateSelector. Variable names should suggest a property or noun, such as
userName, or Width. Use pronounceable names, such as User, not usr. Use
descriptive names for variables used globally, use short names for variables used locally.


Be consistent and use parallelism. If you are abbreviating number as num, always use
that abbreviation. Don't switch to using no or nmbr.


Values that are treated as constants, that is, not changed by the program, should be
declared in the beginning of the scope in which they are used. In PHP this is done with
the define function. Each of these constants should be paired with a comment that
explains use. They should be named exclusively with uppercase letters, with underscores
to separate words. You should use constants in place of any arbitrary values to improve
readability.


// maximum length of a name to accept
define("MAX_NAME_LENGTH", 32);
print("Maximum name length is ". MAX_NAME_LENGTH);


Constants that belong to a specific module should use a consistent prefix.


//text with which to label the field
define("ADDR_LABEL", 0);


//name of the form field (sans prefix of course)
define("ADDR_VAR", 1);


//error message to display for missing fields
define("ADDR_ERROR", 2);


Variables are to be declared with the smallest possible scope. This means using function
parameters when it's appropriate.


Lines should not exceed 78 characters. Break long lines at common separators and align
the fragments in an indented block.


if(($size max_size) OR
(isSizeInvalid($size)))

Free download pdf