Chapter 7. Function Reference

This chapter lists many of the most commonly used functions in PHP. Other functions are grouped together according to their topic, throughout this book.

Calling a function in PHP can be as simple as printing the name of a function with two parentheses, "()", after it. However, many functions require you to give them input to work on, called parameters, which you send inside the parentheses. On top of that, nearly all functions have a return value, which is the result that the function sends back to your script. These return values can often be ignored, but most of the time, you will want to store them in a variable for later use:

    $string_length = strlen($mystring);

You can also use these return values as parameters to other functions, like this:

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

Although most parameters are required, some are optional and don't need to be supplied. When optional parameters are omitted, PHP will assume a default value, which is usually good enough.

When you pass a parameter to a function, PHP copies it and uses that copy inside the function. This process is known as pass by value , because it is the value that is sent into the function rather than the variable. This means that when you pass variables to a function, it can change its copies of them however it likes, without affecting the original variables. To change this behavior, you can opt to pass by reference, which works in the same way as reference assigning for variables—PHP passes ...

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.