The community is working on translating this tutorial into Turkish, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".
Functions
A function in PHP, and any other programming language, allows you to encapsulate a piece of code and call it from other parts of your code. This makes it possible to write code which can be re-used from different parts of your application, without actually writing the code over and over again. Here is an example of a simple PHP function:
<?php
function SayHello($to)
{
echo "Hello, ".$to;
}
?>
A function should always start with the keyword function, followed by the name of the function you wish to declare. In this case, we name our function SayHello. It should be followed by a set of parentheses, which allows you to define parameters for the function. Parameters are not required, but if you don't have any, you should still write the parentheses, like SayHello(). With our function, we define the $to parameter. Since PHP is a typeless language, you don't have to define a type for the parameter, and you may send any kind of datatype through the function. The content of our function is between a set of curly braces, in this case a simple echo statement, where we use the $to parameter to send a greeting to a specific person or thing.
You use the function simply by writing the name and the required set of parameters, like this:
<?php
function SayHello($to)
{
echo "Hello, ".$to;
}
SayHello("World");
?>
Functions may return values as well. In the first example we don't, since it's not required, but it is possible and very useful as well. If you have used other programming languages, you might be used to declaring whether or not a function should return anything, and if so, which type it will be. However, this is not needed with PHP. Let's change our function so it returns the required string instead of outputting it:
<?php
function SayHello($to)
{
return "Hello, ".$to;
}
echo SayHello("World");
?>
As you can see, we simply have the function return the string instead of outputting it, and then we do the actual output once we call the function. This is normally how it's done, since outputting stuff directly from the string requires it to be positioned where we want the output, instead of in the top of our document or an include file.
A very important aspect of functions is the scope. Once you enter a function, you enter a new scope, and while functions and classes are automatically global, variables are not. This means that a variable declared outside a function is not automatically available inside it. Try this example to see it for your self:
<?php
$var = "test";
function SayHello()
{
return "Current value of var: ".$var;
}
echo SayHello();
echo "<br /><br />";
echo "Current value of var: ".$var;
?>
The result will be that the first output won't have a value for$var, because it's simply not known within the function. You could declare it inside the function, and assign a value to it, but it would not be reflected outside the function. However, it is possible to get access to the outside variables within a function - you simply need to declare them as globals inside the scope of the function, like this:
<?php
$var = "test";
function SayHello()
{
global $var;
return "Current value of var: ".$var;
}
?>
When declaring a function, you may define one or several parameters to be optional. It's done by giving them a default value in the declaration. More than one parameter can be optional, but only the last X number of parameters can be optional - you can't have an optional parameter followed by a required parameter. In the next example, we define a couple of optional parameters to show you how it's done.
<?php
function GreetCoder($name, $msg = "Hello, coder!")
{
echo "A message to " . $name . ": " . $msg;
}
GreetCoder("John Doe");
echo "<br /><br />";
GreetCoder("John Doe", "This is a custom message!");
?>
As you can see, we can call the function with only one parameter or with both parameters. The output is affected by what we do, but no matter what, $msg will have a value. It can come in handy in a lot of situations, which you will experience your self once you write more PHP.