# Functions in PHP: Return Values and Parameters

Functions are an important part of programming languages. They help us avoid code duplication by allowing us to run the same set of instructions over and over again on different data.

In this tutorial, we will talk about functions in PHP. We will cover all the basic concepts of functions in PHP, and you'll learn how to create your own user-defined functions in PHP. You will learn about returning values from a function and function parameters in PHP. You'll also learn other concepts like setting default argument values or passing an argument by reference.

## Internal Functions in PHP

PHP comes with a lot of built-in functions that you can use in your program. Some of these functions come as standard, while others become available to you through specific PHP extensions.

PHP comes with a lot of functions to work with strings. For example, you can use the str_contains() function to check if a string contains a substring and the wordwrap() function to wrap a string to a given number of characters. These functions are available for you to use as standard.

Another set of PHP functions for manipulating images is available if you install the GD extension library. Once the extension is enabled, you will be able to use functions like imagecreatetruecolor(), imagecreatefrompng(), and imagefill() to create and manipulate images.

If you ever get a fatal undefined function error in PHP but you are certain that it is an internal function, make sure that you have installed the respective extensions to use that function.

## User-Defined Functions in PHP

You can also define your own functions in PHP. Defining your own functions becomes a necessity in almost every non-trivial program you write. They are a great way to avoid code duplication and errors.

Here is some basic code to define a function that outputs a greeting when we call the function later.

In the above example, we just passed a name to the greet() function, and it echoed a different output based on the specified name.

You have to follow specific rules when defining a function. The name of a function has to start with a letter or an underscore. After that, you can use different lengths of numbers, letters, and underscores in the name. This means that greet01(), _greet(), and greet_01() are all valid function names in PHP. However, using 01_greet() as a function name will throw a syntax error.

## Return From a Function in PHP

In the previous example, our user-defined greet() function did not return anything. It just echoed a couple of sentences based on the input. It is entirely up to us to return anything inside any function we define in PHP.

When you want to return something, you need to use the return statement. A function can return any type of value in PHP. This includes strings, integers, floats, arrays, or even objects.

In the above example, the palindrome() function turns any given string into a palindrome by appending it to its reversed version. Now, instead of echoing the output directly, we assign it to a variable. We can now further manipulate the variable, but we'll just echo it in this example.

A function can have multiple return values, and the execution of the function stops immediately once it encounters a return statement. This means that you can only return one value from a function. However, you can work around this limitation by returning an array that consists of all the values that you want to return as its elements.

In the above example, we wanted the function to return a bunch of information about the name passed to it. So we used an array with mixed values. The passed values are then assigned to different variables using list().

You should also note that the greet() function did not echo Bye Monty! in our program. This is because the function execution was stopped as soon as it encountered a return statement.

## Function Parameters in PHP

Function parameters (also known as "function arguments") are used to pass an input to a function, upon which it can act to give us the desired output. We have only defined functions with a single parameter so far in this tutorial.

However, it is entirely up to us how many parameters we want to pass to a function. It can be zero for functions which don't require any kind of input from us. It can also be two, three, five, or ten. You can also pass arrays as parameters.

Here is an example of PHP functions that accept zero and two parameters respectively.

We can rewrite the second function to pass an array as our only parameter. We will just extract different values from the array inside our function. This can be helpful in situations where you want to either manipulate arrays or pass a lot of values to a function.

## Conclusion

In this tutorial, we have covered a lot of the basics related to functions in PHP. Hopefully, you can now write your own user-defined functions that take advantage of everything we learned here. Feel free to ask any questions you have about this topic in the comments.

### Learn PHP With a Free Online Course

If you want to learn PHP, check out our free online course on PHP fundamentals!

In this course, you'll learn the fundamentals of PHP programming. You'll start with the basics, learning how PHP works and writing simple PHP loops and functions. Then you'll build up to coding classes for simple object-oriented programming (OOP). Along the way, you'll learn all the most important skills for writing apps for the web: you'll get a chance to practice responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database.