PHP functions.

PHP functions
PHP function definitions and calls are similar in many ways to C++'s syntax.
PHP has clarified the syntax in several ways.
function function_name ( formal_arguments ) {

  // function code

}
Rules Governing Formal Arguments
Argument names represent "local" variables inside the body scope of the function.
Arguments by default are "passed by value"; that is, a copy of the actual argument is stored in the specified local variable name. This includes arrays (unlike C/C++ that pass arrays "by address").
To pass an argument "by address" (reference), precede the formal argument with an ampersand (&).
Default values can be assigned to trailing arguments by following the argument name with an equal sign (=) and the value. Passed by address arguments cannot include default values.

Returning Values and Invoking Functions
Similar to C/C++, the return statement can be used to return values from a PHP function. Unlike C/C++, however, PHP functions can return arrays. Functions also do not have to return anything (generally known as a "procedure").
To invoke (call) a PHP function, you merely have to use the function's name in a statement, or as a statement by itself, and pass the required number of actual parameters. Again as with C/C++, you can assign the returned value of a function to another variable, or ignore the returned value.
function example1 ($arg1, $arg2) {
  $sum = $arg1 + $arg2;
  return $sum;
} // example1

function example2 ($numArray, &$sum) {
  $sum = 0;
  foreach ($numArray as $value)
    $sum += $value;
} // example2

function example3 ($numArray, $total=0) {
  foreach ($numArray as $value)
    $total += $value;
  return $total;
} // example3

function example4 (&$array2x, $row=3) {
  $result = array();
  foreach ($array2x[$row] as $value)
    $result[] = $value;
  return $result;
} // example4


$total = example1($num1,$num2);
echo 'The total is' . example1(12,14) . "<br>\n";

$values = array (1,2,7,6,3,2);
example2 ($values,$sum);
echo "The sum is $sum<br>\n";

$total = example3(array(7,10,-3,14)) + example3($values,100);
echo "The new total is $total<br>\n";

$a2x = array (array(1,2,3,4,5), // row 0
              array(6,5,4,2,3), // row 1
              array(9,3,4,5,6), // row 2
              array(7,6,2,9,8), // row 3
              );
$row2 = example4($a2x,2);
print_r ($row2);


The total is 26 The sum is 21 The new total is 149 Array ( [0] => 9 [1] => 3 [2] => 4 [3] => 5 [4] => 6 )

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.
function printValues ($values) {
  if (is_array($values)) 
    foreach ($values as $value) echo "$value<br>\n";
  else echo "$values<br>\n";
} // printValues
Although the above example is rather simple, the physical handling of the two types of arguments (a single value vs the array) can be more complicated, requiring alot of duplicated code. The duplicated effort can be easily eliminated by using a simple type-modification trick; that is, if the argument is not an array, make it one, before you do your work.
function printValues ($values) {
  if (!is_array($values)) $values = array($values);
  foreach ($values as $value) echo "$value<br>\n";
} // printValues

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
Therefore, the only ways to access global data from inside a function in PHP are:
  • Pass the global value as a parameter
  • Reference the global data in a global statement inside the function definition
  • Reference the corresponding variable name index in the super global $GLOBALS associative array
Share on Google Plus

About M

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment