#5 Functions in PHP

In this php class I will explain in a simple way what are functions in phpalso what are the functions for? and finally through basic examples how to create functions in php

Content

  • What is a function in PHP
  • What are the functions in php for?
  • How to create functions in php
  • Example of functions in php
  • function arguments
  • return values
  • Types of functions

What is a function in PHP

The functions or procedures in PHP They are a feature of programming languages ​​by which we programmers can assign a name to a piece of code and call it later.

Functions also allow additional concepts to add complexity to the code, such as passing variables to that piece of code or even having that named code return information to the place where it was called.

Functions are one of the mechanisms that programming has to bypass sequential programming. Thanks to a mastery of its possibilities we will be able to program fewer lines to enhance the same.

What are the functions in php for?

The functions in php, and in programming in general, are a tool with many advantages:

  • We can skip the natural order of code execution (sequential) to call the functions that may be created in other files or in areas that we prepare for it.
  • They allow us to achieve a much more organized code: we will have pieces of code declared only once when without the functions they would be repeated over and over again throughout the code of our applications. Also, if the names of the functions are clear enough, we won’t need to place comments in the code areas where they are used.
  • They simplify our code, making it more readable and easy to maintain, either by us or by another programmer involved in the development.
  • With functions we can create solutions to previously unattainable problems.

How to create functions in php

Creating functions in php is easyAs I have mentioned before, creating functions is nothing more than giving a name to some lines of code to be able to call them later. To name the lines of code with PHP and define them as a function we will do the following:

#php
function name_of_my_function() {
//lines of code that belong to the function
}
#/php

writing the reserved word function followed by function name and the parentheses, we declare the functionand by using braces we declare which lines of code belong to the function.

The name of a function must always begin with a letter.. Furthermore, it cannot contain hyphens or strange characters, only letters (uppercase or lowercase) and numbers, accompanied at most by an underscore as in the example.

Example of functions in php

In the following example I am going to create a function that displays “hello world” on the screen:

#php
function first_function() {
echo ‘Hello world’;
}
#/php

To launch the function I just have to indicate its name and parentheses:

#php
first_function();
#/php

Calling the function by its name will execute its code, displaying the text “Hello world” to the browser.

function arguments

One of the most notable features of the functions is the possibility of receiving information with each call. We can then perform actions with that information, which will vary depending on the call. The number of input parameters is unlimited.

Let’s see an example that explains what we’ve seen so far:

#php
function with_parameters ( $var1, $var2, $var3 ) {
$sum = $var1 + $var2 + $var3;
echo ‘The sum of the three numbers is : ‘ . $sum . ‘#lt;br/#gt;’;
}
#/php

at the time of call function we will have to indicate values ​​for the input parameters. If we leave any input parameter without value, we will be incurring a slight error, but in the worst case it will prevent the function from being executed correctly. In the case of having enabled the display of errors, a message will appear. Warning.

To call the function with_parameter() I will write the following in the script:

#php
with_parameters( 1, 2, 3 );
with_parameters( 24, 33, 10 );
#/php

I have made two calls to the functionwith values ​​for different input parameters: for $var1 1 and 24, for $var2 2 and 33, and in $var3 3 and 10. The results shown in the browser when viewing the files would be:

6

67

return values

Other basic functionality of functions is the possibility of return a value at the end the execution of the code. We can store the returned value or not without incurring an error. For return values we will do it with the return keyword along with the value to return, which can be a variable of any type, a function call, an operation, etc.

The following function returns the result of multiplying the input variables:

#php
function multiply ( $num1, $num2 ) {
return $num1 * $num2;
}
#/php

Now to call the function and save the return value is as simple as:

#php
$result = multiply ( 3, 9 ) ;
echo $result;
#/php

in variable $result I assign the value of the result of the multiply function for the values ​​3 ($num1) and 9 ($num2). When performing the echo, we then display the result on the screen:

27

Types of functions

Have you seen the basic operation of creating functions in phpspecifically they are User-defined functions, but in php there are several other types of functions. Specifically, these types are:

  • User-defined functions
  • variable functions
  • Internal functions (included)
  • anonymous functions

In the links you will find concise explanations of the official php documentation. In the following articles I will explain in more detail each of the types and some additional features of the functions.

Leave a Reply