PHP Function

A function in PHP is created using keyword "function" followed by the function name which must start with a letter or underscore. We then use round brackets after function's name and provide optional arguments within round brackets, separated by comma. Curly brackets follow which indicate the start and end of the function's code. All code that will be performed by the function go inside the curly brackets.

Arguments vs Parameters

Let's clarify a subtle difference between arguments and parameters. Arguments are variables which are future placeholders for values, those values will be assigned when the function is called. Parameters on the other hand are values for those arguments defined in the function. Parameters must have values, so they can also be named expressions, because in PHP an expression is anything that expresses a value.

Many programmers understandably refer to arguments and parameters as synonyms because they are so similar, however I think it's important to get the lingo right from the beginning when learning a programming language because you'll avoid a lot of confusion in the future.

//$one and $two are arguments function multiply( $one, $two ) //function definition { return $one * $two; } //5, 5 are called parameters $multiply_result = multiply( 5, 5 ); //function call echo $multiply_result; //output: 25

Returning values from functions (using keyword return) can be useful in case we want to store information in a variable or modify it later in the program. Another way is to directly output the result from within the function:

function multiply_print( $one, $two ) { echo $one * $two; //printing values directly } multiply_print( 10, 5 ); //output: 50

Default Values

Function's arguments can also have default values, which we assign during function's definition:

function multiply_default($one = 5, $two = 10) { echo $one * $two; }

Now multiply_default() function can be called without specifying any parameters

multiply_default(); //output: 50

Passing parameters to a function which has default argument values will overwrite those values:

multiply_default( 4, 4 ); //output: 16

Type Declarations

We can set specific data type arguments when writing a function declaration. In the below example, the only value that will be accepted is of data type array.

function print_info(array $info) { print_r($info); } $my_info = array('name' => 'Vladi', 'age' => 33, 'gender' => 'M', 'job' => 'Clever Techie'); print_info($my_info); //output: Array ( [name] => Vladi [age] => 33 [gender] => M [job] => Clever Techie )

Arguments by Reference

Whenever we work with data inside the function, that data, will not be available outside of the function, unless the value is returned. However, there is another way of keeping whatever data we have modified inside the function - passing function arguments by reference!

First, this is what would happen if we didn't pass this one argument by reference:

function add_text( $string ) { //.= means concatenate string or add string to a string $string .= " and sometimes I like programming"; echo $string; //outputs: "My name is Vladi and sometimes I like programming" } $my_text = "My name is Vladi"; add_text( $my_text); echo $my_text; //outputs: "My name is Vladi";

So when we print the value of $string inside the function, we get the expected result, with extra text, but when we try to echo out the text after calling the function, outside of it, then only the original part is printed.

But....but...what if I want that extra text and print it outside the function as too? Well then... you need to pass function arguments by reference of course! All we need to do is add an ampersand & in front of the function argument, it's that easy!

function add_text( &$string ) { $string .= " and sometimes I like programming"; } $my_text = "My name is Vladi"; add_text( $my_text); echo $my_text; //outputs: My name is Vladi and sometimes I like programming


Don't worry...a recursive function is just a function that calls itself, remember to always have an exit when defining such function, otherwise it will run forever (just like an endless loop).

function recursion($a) { if ($a <= 20) //exits when $a is greater than 20 { echo "$a\n"; recursion($a + 1); } }

User-Defined vs Built-in Functions

We have defined many functions throughout this lesson, those are all our precious user-defined functions. There is also a library of functions that are already created and come with PHP called built-in functions. Here are some common built-in functions:

print_r ( $array ); //prints out the array count ( $array ); //counts the number of elements in an array strtolower ( $string ); //converts a STRING to lower uppercase letters: string

Let's Get Practical

So what would be a good example of a user-defined function that we could use in a real virtual life? Let me find something....

function print_file_names_from_directory( $dir_path ) { $files = scandir( $dir_path ); $files = array_diff($files, array('..', '.')); $files = array_values($files); for ($i = 0; $i < count($files); $i++) { echo $files[$i].'<br />'; } }

The above function will take in a directory path and print out all the files from that directory. Notice how I used four different built-in functions: scandir(), array_diff(), array_values(), count() to accomplish the task. Don't worry about what they all mean at this point if you don't understand them.

New Medical Assisstant Schools