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

4.2     How to read function prototypes

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

The standardised way to describe the parameters accepted and value returned from a function is through a function prototype. They are used throughout this book and also online in the PHP manual. Once you are practised in the art of reading a function prototype, you should be able to have a rough understanding of how to use a function just by reading its prototype. However, you need not understand how function prototypes work in order to read this book - all the functions used are explained textually, with the prototypes presented merely for convenience.

As they are used for each function shown in this book, it is important you understand them before proceeding. Do not worry what each function does right now, as they will each be explained in the coming pages. I have taken each function prototype directly from the PHP manual so that you should be able to flick to and from the manual easily. Here is the prototype for a basic function, strtoupper():

string strtoupper ( string string)

What that means is that the function takes one string and returns a string. The reason it says "string string" is because the PHP manual usually describes what each parameter does, and so it names each parameter. Here it is telling us that strtoupper() takes one parameter, a string, which is called "string" - not the best of naming schemes, I agree, but it makes sense. It also returns a string - the manual puts the return type before the function name, as with most C-like languages.

If a function uses a parameter as a reference, that parameter will be preceded in the prototype by an ampersand. This does not mean you have to use a prototype in your function call, it is just there to remind you that the parameter is a reference.

Now consider the prototype for sha1():

string sha1 ( string source [, bool raw_output])

As you can see, sha1() returns a string, takes a string as its first parameter, with a boolean as its second parameter. However, the boolean is in square brackets - this has a special meaning, which is "this is an optional parameter".

The optional parameters can sometimes appear a little confusing, as is amply demonstrated by the definition for mysql_connect():

resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]])

Every parameter in mysql_connect() is optional because PHP has default values for each of them if you do not provide them. The prototypes also include two special data types: "number", and "mixed". A number is any floating-point or integer number, and "mixed" means it accepts or returns two or more data types.

The definition of the pow() function, for example, is this:

number pow ( number base, number exp)

It accepts any numbers and can return any numbers. The definition of serialize(), however, is this:

string serialize ( mixed value)

When you get to work using serialize(), you will find that it works with both objects and classes, which is why the parameter to serialize() is "mixed". There is also one data type called "void", which means "no return value" or "no parameters taken".

The final convention used in function prototypes is "...", which means "a variable number of parameters are taken". For example, the prototype for the isset() function is this:

bool isset ( mixed var [, mixed var [, mixed ...]])

There it takes a minimum of one variable, but it can accept many because of the optional "...".





<< 4.1 Functions overview   4.3 Working with variables: isset(), empty(), and unset() >>
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
A PHP User - 13 Oct 2008

Thank you for this book, you are a God among men.

That said, even with several years of programming under my belt this is hard to follow, BUT that's because you need a good editor. It's far better than what I could ever do, thats for sure.

As to anyone who thinks this book is too long.... have you ever read a book? Because generally they tend to be longer than your average online blog. If this book was in print it would be just as big as any other programming book.

A PHP User - 13 Oct 2008

Thank you for this book, you are a God among men.

That said, even with several years of programming under my belt this is hard to follow, BUT that's because you need a good editor. It's far better than what I could ever do, thats for sure.

As to anyone who thinks this book is too long.... have you ever read a book? Because generally they tend to be longer than your average online blog. If this book was in print it would be just as big as any other programming book.

A PHP User - 13 Oct 2008

totally new to programming but want to learn php badly. you had me going until this page in the book and my brain froze. but all and all i'll be back to learn more

A PHP User - 13 Oct 2008

if you don't understand this page like most people including me. The best thing to do is bookmark and come back later. Things have always clarified that way for me so far.

A PHP User - 13 Oct 2008

http://www.usphp.com/about.prototypes.html

A PHP User - 13 Oct 2008

http://www.usphp.com/about.prototypes.html

A PHP User - 13 Oct 2008

http://www.usphp.com/about.prototypes.html

kewlceo - 13 Oct 2008

Yes, it is a function to convert case, but Paul is illustrating the prototype, or template, if you will.

string strtoupper ( string string ) // the template

means that strtoupper takes a string named "string" and returns the result to "string", thus the actual syntax:

$str = strtoupper($str);

A PHP User - 13 Oct 2008

"What that means is that the function takes one string and returns a string." --> Actually function takes lower case and upper case letters string and change them all to upper case letters string.

So far this book is great!

A PHP User - 13 Oct 2008

"What that means is that the function takes one string and returns a string." --> Actually function takes lower case and upper case letters string and change them all to upper case letters string.

So far this book is great!

A PHP User - 13 Oct 2008

i'm lost...

A PHP User - 13 Oct 2008

Whats the best way to approach programming so you can learn?
This is the second time I've read this. I must be phick.

Indo - 13 Oct 2008

"this page does seem to be a little early in the book...
simple protptypes are OK but optionalk arguments are a bit much for beginners...

otherwise this book seems to be an excellent book on the language."
I don't think so. I only have messed with Unix and Linux for a few years. I learned Basic back in the 5th grade, beyond that I don't have any programming experiance. But this page was pretty easy.

Dominic Son - 13 Oct 2008

This page does look complicated to any beginner (including myself), but i suspect mr hubzilla is just demonstrating how complicated a function can look, and how you would read it, not so much concerned with what all this stuff does so far.

If your head hurts, don't trip, just focus on the examples in the dotted line, and ask yourself 'how would php read this function? and that's where hubber explains how - as the title suggests..

zephproj@yahoo.com - 13 Oct 2008

Negative comment: So far, I find this book to be great but not quite on this section.

Positive comment: This tutorial is still the best tutorial I have ever encountered. This tutorial has drawbacks and so with other tutorials, no tutorial is perfect.

More comments: I consider individuals/institutions who offers free resources on the internet as "Heroes of the Internet", may they multiply.

zephproj@yahoo.com - 13 Oct 2008

Negative comment: So far, I find this book to be great but not quite on this section.

Positive comment: This tutorial is still the best tutorial I have ever encountered. This tutorial has drawbacks and so with other tutorials, no tutorial is perfect.

More comments: I consider individuals/institutions who offers free resources on the internet as "Heroes of the Internet", may they multiply.

A PHP User - 13 Oct 2008

this page does seem to be a little early in the book...
simple protptypes are OK but optionalk arguments are a bit much for beginners...

otherwise this book seems to be an excellent book on the language.

TxtEdMacs - 13 Oct 2008

"Why (snip)

not written as:
resource mysql_connect ( [string server], [string username], [string password] ,[bool new_link], [int client_flags])"

If you do not include the form: ...[, ... it is not a condition inclusion. The form above means all those parameters should be included. Moreover, what role does the [...], [...], [...], play? None that I can see, since the square brackets indicat nothing in such a snytax.

A PHP User - 13 Oct 2008

A PHP User wrote:

"Why is:
resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]])

not written as:
resource mysql_connect ( [string server], [string username], [string password] ,[bool new_link], [int client_flags])"

The reason it is written this way is one of the restrains of optional parameters in C syntax languages. If you want to include an optional argument, you must include all the arguments before it.

Let's say for instance that the default server is "myServer", the default username is "admin", and the default password is "1234". If I wanted to give a different password, I couldn't just call mysql_connect("4567") because the function would think I was passing the first argument (server), mysql_connect(,,"4567") wouldn't work either. You would have to call the function with mysql_connect("myServer", "admin", "4567") even though the first two arguments are the defaults.

The nested brackets are a reminder that you have to include all the preceding paramaters. It is ugly to look at, but there is a method to the madness

rolo@irolo.net

Shadow - 13 Oct 2008

If you are using functions within a beginners book...Then you are really defeating the purpose...Because noobs are soupposed to learn...But it might be a little hard for them to understand..Since you haven't explained how they work..

Another PHP User - 13 Oct 2008

My head hurts too!!!

I reckon this page may need a re-write, its too complicated/technical. The use of the functions from the manual was confusing rather than helpful because they contained stuff we havnt seen yet which just threw me.
Also being at the start of the chapter, this page really puts you of reading any further!

Thanks for a great book though, its been really helpful so far :)

A PHP User - 13 Oct 2008

my head hurts.

A PHP User - 13 Oct 2008

Why is:
resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]])

not written as:
resource mysql_connect ( [string server], [string username], [string password] ,[bool new_link], [int client_flags])

A PHP User - 13 Oct 2008

"However, you need not understand how function prototypes work in order to read this book - all the functions used are explained textually, with the prototypes presented merely for convenience.

As they are used for each function shown in this book, it is important you understand them before proceeding."

These sentences directly follow each other above but seem to contradict!

A PHP User - 13 Oct 2008

Good stuff



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 three plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow