PHP functions
An Interesting PHP Data-Manipulation Technique
|
PHP's ability to change a variable's type "on-the-fly" can provide some
interesting solutions to some classic coding problems. For example, let's say you needed a general-purpose function that would process either a single value argument, or an array of arguments. In the design of your function interface, type-specific languages would normally require the writing of two separate functions to handle the two distinct types of input data, or require the user to (somehow) (at least temporarily) convert the single value into an array. PHP could easily solve this problem with a single function.
|
Referencing Global Data Inside a Function
In C/C++, it is easy to access global data inside a function; you merely
have to reference the global variable name. In PHP, however, you must
identify each global variable with a global reference.
$gNum1 = 'test';
$gNum2 = 27;
function example1 ($parm) {
global $gNum1, $gNum2;
echo $local = "$gNum1 $parm";
$gNum2 = 'something else';
} // example1
|
- Pass the global value as a parameter
- Reference the global data in a
globalstatement inside the function definition - Reference the corresponding variable name index in the super global
$GLOBALSassociative array


0 comments:
Post a Comment