Dark Launch

This is a Dark Launch.

JavaScript Hungarian Notation Variable Prefix Naming Convention

Hungarian Notation is a language agnostic naming convention that prefixes variables with the variable's type. This allows the reader to determine the type and use of a variable by such an identifier.

JavaScript Hungarian Prefixes:

  • a - array
  • b - boolean
  • f - float
  • fn - function
  • i - integer
  • n - node
  • o - object
  • s - string
Javascript
var aData = [1, 2, 3];
var bFound = false;
var fGoldenRatio = 1.618;
var fnCallback = function() { };
var iCurrentPage = 1;
var nNewRow = document.createElement("tr");
var oSettings = {
    type: "GET",
    url: "test.json",
    dataType: "jsonp"
};
var sLabel = "First Name";

Prefixes may also be combined where appropriate.

Javascript
var asData = "foo,bar,baz".split(","); // Array of type String.

Examples in other languages:

C++
int iNumber = 2;
PHP
$sQuote = "Imagination is more important than knowledge.";
AutoIt
$sMsg = "Example message string."

PHP Return Function Response to Variable rather than Print or Echo it

To capture the function's response or output to a variable, use the following function:

PHP
function get_contents($function) {
    ob_start();
    $function();
    $contents = ob_get_contents();
    ob_end_clean();
 
    return $contents;
}

To use, pass an anonymous function to get_contents().

Examples:

PHP
// Example: get function's response to variable.
function add($this, $that) {
    echo $this . ' + ' . $that . ' = ' . ($this + $that);
}
 
$example = get_contents(function() {
    add(1, 1);
});
 
echo $example . "\n"; // 1 + 1 = 2
PHP
// Example 2: get function's response using return. Even if the function echos output, it can be captured to a variable.
// Compare print_r() that has a return parameter to var_dump() that has no such parameter.
 
$return = true;
$example2 = print_r(array('a', 'b', 'c'), $return);
 
// Solution: use get_contents() to capture the function's response to a variable.
$example3 = get_contents(function() {
    var_dump(array('a', 'b', 'c'));
});
 
echo $example3;
// array(3) {
//   [0]=>
//   string(1) "a"
//   [1]=>
//   string(1) "b"
//   [2]=>
//   string(1) "c"
// }

Note: Anonymous functions are available since PHP 5.3.0.