PHP: Functions

This entry is part 29 of 54 in the series PHP Tutorial

Similar to other programming languages, functions are blocks of code that together achieve a more complex task.

Functions are not executed until you call upon them.

/* Defining a PHP Function */
function writeMsg($name)
  echo "Hi, $name\n";  
  echo "How is life?";
/* Calling a PHP Function */
$john = "John";

In this example, we have passed a parameter John into the function writeMsg.

Note also that in PHP, function names are case insensitive.

If the function only returns a value, we can use the return statement.


 function sum($x,$y)
  return $z;

  echo "8 + 10 = " . sum(5,10) . "\n";
  echo "2 + 13 = " . sum(7,13) . "\n";


Series Navigation<< PHP: For loop
PHP: Declaring functions >>

Leave a comment

Leave a Reply