Archive for January, 2010
min() and max()
Find either the lowest or highest value in an array, or list of variables.
File this under “yes, they already have a function for that” in your memory.
uniqid()
Create a 14 character string based on the current time in microseconds. Optionally add a prefix of your choice (up to 114 characters) and set the second parameter to true for an additional 10 characters if you desire more entropy.
constant()
Returns the value of a constant. While you can simply use the constant’s name in an echo statement for example, this function can be used if the constant’s name is stored in a variable.
defined()
Determines whether or not a constant has been defined. Returns a boolean (true or false). Used only for constants - not variables, functions or anything else. Pass the name of the constant as a string.
define()
Creates a constant. Its value can be only null or scalar (integer, float, string, boolean). That means a constant can not be an array. A constant’s value can not be changed once defined.
Coding Standards
Indention: spaces or tabs? Quotes: single or double? Variable names: camelCase or underscore_style?
There are many things to consider when it comes to best practice for coding standards. Which way is *the* best? The answer is.. whatever the project manager says. Keep it consistent throughout the project; that’s the best standard by far.
Nowdoc
As double quotes are to heredoc, single quotes are to nowdoc. Use nowdoc in the same way as heredoc only enclose the opening identifier (not including the triple less than) in single quotes. This string will not be interpreted and nothing will be parsed. (as of PHP 5.3)
Heredoc
Think of heredoc as double quotes (variables and special characters will be parsed) only you don’t need to escape any double quotes. Begin with a triple less than (<<<) followed by an indentifier and a new line. Next goes the string itself. At the end, a new line and the identifier again (not indented) followed by a semicolon.
Double Quotes
Text inside of double quotes will be evaluated. Variables will be parsed and special characters will be interpreted. Specify a double quote inside of the string by escaping it with a backslash. Specify a backslash by escaping it with a backslash. The backslash also indicates special characters; using it inside of a double quoted string may produce undesired results.
Single Quotes
Text inside of single quotes (’) will be interpreted as text only. Specify a single quote inside of the string by escaping it with a backslash. Specify the combination backslash, single quote by also escaping the backslash with a backslash.
