User defined functions

ad, authenticate, script, geeks corner, json, image, picture, server, file, write, read, close, fread, readfile, fclose, walden, systems, walden systems, php, functions, variable arguments
PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.

A function is a block of organized, reusable code that is used to perform a single action. Functions provide better modularity for your application and allows us to reuse code. As you already know, PHP gives us many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions. There are two things that we have to deal with when dealing with functions, creating and calling functions.

Creating a function is straight forward in PHP, we use the keyword function to declare a function. If you coming from the Python world def. When declaring functions, we can pass arguments and have return values. We can not only declare what arguments to pass but also whether the argument is optional or not. In both PHP and Python, we can declare optional arguments simply by assigning them values. In PHP, to pass an argument by value, we precede he argument name with a "$". In Python, we simply use the argument name.

PHP

1    function printMe($param1, $param2 = "world") 
2    {
3        print $param1 . " " . $param2;
4        
5    }
6         
7    printMe("Hello");
8    printMe("Hello", "John");


Line 7 will output "Hello World" and Line 8 will output "Hello John"


Python
1    def printMe(param1, param2 = "world") :
2        print param1 + " " + param2   
3        return
4    printMe("Hello");
5    printMe("Hello", "John");


Line 7 will output "Hello World" and Line 8 will output "Hello John"


In PHP, we can pass arguments by either value or reference. When we pass an argument by value, the argument value isn't changed by the function. When we pass an argument by reference, the reference to the variable is manipulated by the function rather than a copy of the variable's value. Any changes made to an argument in these cases will change the value of the original variable. We can pass an argument by reference by adding an "&" to the variable name in either the function call or the function definition. In Python, all parameters are passed by reference but whether the changes changes the original variable depends on the type of variable. For example, in Python, if you pass a list, the list can be modified by the function but the same does not hold true for a string.

PHP

1    function addOne($num) 
2    {
3        $num += 1;
4    }
5         
6    function addTen(&$num) 
7    {
8        $num += 10;
9    }
10         
11   $orignum = 10;
12   addFive( $orignum );
13         
14   print $orignum;
15         
16   addSix( $orignum );
17   print $orignum;


The output of line 14 is 10 and the output of line 17 is 20.

Python
1    def change_list_contents(the_list):
2        the_list.append('four')
3        print('changed to', the_list)
4
5   orig_list = ['one', 'two', 'three']
6   print( orig_list ) 
7   change_list_contents( list ) 
8   print( orig_list ) 
Line will output ['one', 'two', 'three', 'four'].

1    def change_string(the_string):
2        the_string = 'Hello John'
3
4    orig_string = 'Hello World'
5    print(outer_string)
6    change_string(outer_string)
7    print(outer_string)


The output of line 7 is "Hello World."

In some cases, we don't know the number of arguments that the function will be receiving, PHP accommodates us by allowing variable arguments. In PHP, we precede "..." before the argument name. In Python, we precede a "*" before the argument name. Java also uses the "..." before the argument name. We can access the variable arguments by iterating through them. To iterate through the variable argument list, we can use a for or for each loop. Below are some implementations of variable arguments.

PHP

1    function add(...$numbers) 
2    {  
3        $sum = 0;  
4        foreach ($numbers as $n) 
5        {  
6            $sum += $n;  
7        }  
8        return $sum;  
9    }  
10  
11   echo add(1, 2, 3, 4); 


The foreach in line 4 iterates through all the items in $numbers.


Python
1    def printNumbers( *numbers ):
2        for number in numbers:
3            print number
4    return;
5
6    printNumbers( 10 )
7    printNumbers( 70, 60, 50 )


The for in line 2 iterates through all the items in $numbers.

Creating user defined functions are similar whether you are in Python or PHP. Python and PHP both have similar methods of creating user defined functions, the only difference is the keyword used to define a function. The way parameters is the tricky part when coming from Python since Python has the nuance of passing all parameters by reference and PHP gives us the choice to pass by reference or by value. Passing variable length arguments are also just a matter of the keywords used so should be familiar to anyone coming from Python.