Hudzilla.org - the homepage of Paul Hudson
Contents > Functions Wish List | Report Bug | About Me ]

4.1     Functions overview

This is NOT the latest copy of this book; click here for the latest version.

To begin with, we are going to look at how to use PHP's built-in functions inside your scripts, as you need to be able to call functions before you need to bother learning how to write your own functions!

Calling a function in PHP can be as simple as printing the name of a function with two empty brackets, ( and ), after it. Many functions take parameters - values that are used to affect the execution of the function. Furthermore, nearly all functions have a return value - the result of the function - and you can use these return values as parameters to other functions, like this:

func1(func2(func3(), func4()));

Author's Note: Parameters are the variables listed in a function declaration that define what values must be passed in to a function, whereas the actual parameters passed in during a function call are called arguments. Many people use "parameter" and "argument" to mean the same thing, whereas others abide strictly by the technical definition. This book uses the two interchangeably, so you need not worry about the difference.

While most parameters are required, some are optional - that is, you do not need to supply them. When optional parameters are not supplied, PHP will assume a default value, which is usually the most commonly desired.

When you pass a parameter to a function, PHP actually takes a copy, and uses the copy inside the function. This means that variables you have passed into a function can be changed as often as you like inside that function and they will remain untouched outside the function. To change this behaviour, you can opt to pass a variable in as a reference, which works in the same way as reference assigning - PHP passes the actual variable into the function, and any changes you make to that variable will still be there when the function exits. This script demonstrates the difference:

<?php
    somefunc
($foo);
    
somefunc($foo, $bar);
    
somefunc($foo, &$bar);
    
somefunc(&$foo, &$bar);
?>

The first line calls somefunc(), passing in a copy of $foo, the second passes in copies of $foo and $bar, the third passes in a copy of $foo but the original $bar, and the last passes in both the original $foo and $bar. Passing by reference, as with $bar in line three and $foo and $bar in line four, mean that these variables can be changed inside the function, which is often used as a way for functions to return values.





<< 4 Functions   4.2 How to read function prototypes >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
akhtardaha@gmail.com - 20 Aug 2008

thats great. very nice guidance is provided here.
i am really impressed.

A PHP User - 20 Aug 2008

Ooohhhhhhh. Phil nice one :)

Steve - 20 Aug 2008

Thank you Phil. I had no idea what Paul was getting at without your help. This FREE book is much appreciated, though!

ajekigbe - 20 Aug 2008

To: A PHP User,

I think he commucates.

"Parentheses" are also "Brackets".

"[]" = Square Brackets.
"<>" = Angle Brackets.
May be "()" = Circle Brackets I am not sure!

A PHP User - 20 Aug 2008

fun reading so far! however, what you call "brackets" are actually "parentheses": '()' are parentheses, '[]' brackets, and '<>' angle brackets. ;)

Btw0 - 20 Aug 2008

You made things just so easy.

Greets from People's Republic of China.

Phil Resch - 20 Aug 2008

To l_padmapriya@rediffmail.com:

The somefunc example, using $foo and $bar, is meant to show you differences in how you can pass information into your function.

He shows two basic ways: pass-by-value (passing a copy of your variable that only exists within the function) and pass-by-reference (passing the actual variable).

Let's look at the third example:

somefunc($foo, &$bar);

The first parameter is being passed by value, so we're sending a copy of $foo into the function. The second parameter is being passed by reference, so we're sending $bar (the actual thing, not just a copy) into the function.

And let's say that we've defined $foo = 3 and $bar = 6. And let's also say that within somefunc() are the lines reading

$foo++;
$bar++;

So when we call somefunc() with $foo and $bar ...

Inside of the function the ++ operator will increment the copy of $foo, so the copy of $foo == 4.

The ++ operator will increment the actual $bar variable, so $bar == 7.

Then all of the code inside of somefun() finishes execution, and we leave the function. The copy of $foo gets destroyed, because it existed only inside of the function.

So after somefunc() executed we're left with $foo == 3 (because the original was never changed, only a copy of it was changed) and $bar == 7 (because the original was changed inside of the function).

Phil Resch - 20 Aug 2008

To l_padmapriya@rediffmail.com:

The somefunc example, using $foo and $bar, is meant to show you differences in how you can pass information into your function.

He shows two basic ways: pass-by-value (passing a copy of your variable that only exists within the function) and pass-by-reference (passing the actual variable).

Let's look at the third example:

somefunc($foo, &$bar);

The first parameter is being passed by value, so we're sending a copy of $foo into the function. The second parameter is being passed by reference, so we're sending $bar (the actual thing, not just a copy) into the function.

And let's say that we've defined $foo = 3 and $bar = 6. And let's also say that within somefunc() are the lines reading

$foo++;
$bar++;

So when we call somefunc() with $foo and $bar ...

Inside of the function the ++ operator will increment the copy of $foo, so the copy of $foo == 4.

The ++ operator will increment the actual $bar variable, so $bar == 7.

Then all of the code inside of somefun() finishes execution, and we leave the function. The copy of $foo gets destroyed, because it existed only inside of the function.

So after somefunc() executed we're left with $foo == 3 (because the original was never changed, only a copy of it was changed) and $bar == 7 (because the original was changed inside of the function).

MrGonzy - 20 Aug 2008

Really good online book.

I had c++ in highschool and I have to say that i learn a lot and very quick here.

Things for me to do: Buy your book !!!

Thanks
And greets from Belgium

l_padmapriya@rediffmail.com - 20 Aug 2008

The example foo, bar function confuse me litte bit. can you help me?

Learning PHP - 20 Aug 2008

Wow, I'm surprised how much like C++ PHP is, it's great.

For Jordan:
The function call at the top of the page is a little confusing but I think that was done to show PHP's capability to use function return values as arguments in another function.

For example:

func1(func2(func3(), func4()));

func1() in this case takes 1 argument which is the value returned by func2. func2 can take 2 arguments which are the values returned by func3() and func4() and must be evaluated inside func2() before it can be used as an argument in func1. You can tell all this because of the way the parenthesis are placed.

If you changed func1 to accept 2 parameters and func2 to accept 1 parameter the syntax would look something like this.

func1(func2(func3), func4());

I hope this helps Jordan.

Jordan - 20 Aug 2008

Hmmm... this page has confused me somewhat.

A PHP User - 20 Aug 2008

Great tutorials so far.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow