PHP Functions

A function is a block of program code that can be called from any point in a program. Some programming and scripting languages draw a distinction between a function (which can return a value to the program statement that calls it) and a procedure (or sub-routine) which does not directly return a value. A function can accept parameters (or arguments), and can return a value to the routine that calls it (although it does not actually have to do either). PHP has hundreds of built-in functions, which are documented on the PHP group website (http://www.php.net). In addition, you can define your own functions, which can then be used whenever required within your own scripts. To declare and define a function, use the following syntax:


function functionName()
{
  code to be executed;
}


Function names should reflect the purpose of the function, but should not be too long. A function name may start with a letter or an underscore character, but not a number. The following example defines a function that simply outputs some text:


<html>
  <body>

<?php

  function helloWorld()
  {
    echo "Hello World!";
  }

  helloWorld();
?>

  </body>
</html>


Enter the code above into a text editor and save it with the filename "hello_world.php" in the "htdocs" directory of your "xampp" directory. Run the XAMPP server, then type the following URL into the browser’s address bar:


http://localhost/hello_world.php


You should see output that is something like the screenshot below.


The output from hello_world.php

The output from hello_world.php


Function parameters are arguments that are passed to a function when it is called. Each parameter is a named variable defined within parentheses immediately following the function name. These variable names can then be used within the function body in the same way as variables elsewhere in the code. The function’s output will vary, depending on the values passed to the function as arguments. In the example script below, the function printASCII() prints a range of characters from the ASCII character set. The parameters supplied determine the first and last characters in the range.


<html>
  <body>

<?php
  function printASCII($start, $end)
  {
    echo "Printing ASCII characters ";
    echo $start . " to " . $end . ":<br><br>";
    for ($i=$start; $i<$end; $i++)
    {
      echo "&#" . $i . ";";
    }
  }
  printASCII(32, 127);
?>

  </body>
</html>


Enter the code above into a text editor and save it with the filename "function_parameters.php" in the "htdocs" directory of your "xampp" directory. Run the XAMPP server, then type the following URL into the browser’s address bar:


http://localhost/function_parameters.php


You should see output that is something like the screenshot below (you can vary the value of the parameters to see different output ranges).


The output from function_parameters.php

The output from function_parameters.php


A function can also return a value to the code that calls it. This is achieved by using the return statement, as illustrated in the short script example below, which is a modified version of the previous script in which the printASCII() function not only prints out the specified range of characters, but calculates and returns the number of characters in the range.


<html>
  <body>

<?php

  function printASCII($start, $end)
  {
    echo "Printing ASCII characters ";
    echo $start . " to " . $end . ":<br><br>";
    for ($i=$start-1; $i<$end; $i++)
    {
      echo "&#" . $i . ";";
    }
    return $end - $start + 1;
  }

  $charCount = printASCII(32, 127);

  echo "<br><br>The number of characters in the range is: ";
  echo $charCount;
?>

  </body>
</html>


Enter the code above into a text editor and save it with the filename "function_returnval.php" in the "htdocs" directory of your "xampp" directory. Run the XAMPP server, then type the following URL into the browser’s address bar:


http://localhost/function_returnval.php


You should see output that is something like the screenshot below (you can vary the value of the parameters to see different output ranges).


The output from function_returnval.php

The output from function_returnval.php