Skip to Main Content
Programming PHP, 4th Edition
book

Programming PHP, 4th Edition

by Kevin Tatroe, Peter MacIntyre
March 2020
Intermediate to advanced content levelIntermediate to advanced
542 pages
12h 22m
English
O'Reilly Media, Inc.
Book available
Content preview from Programming PHP, 4th Edition

Chapter 3. Functions

A function is a named block of code that performs a specific task, possibly acting upon a set of values given to it, aka parameters, and possibly returning a single value or set of values in an array. Functions save on compile time—no matter how many times you call them, functions are compiled only once for the page. They also improve reliability by allowing you to fix any bugs in one place rather than everywhere you perform a task, and they improve readability by isolating code that performs specific tasks.

This chapter introduces the syntax of function calls and function definitions and discusses how to manage variables in functions and pass values to functions (including pass-by-value and pass-by-reference). It also covers variable functions and anonymous functions.

Calling a Function

Functions in a PHP program can be built in (or, by being in an extension, effectively built in) or user-defined. Regardless of their source, all functions are evaluated in the same way:

$someValue = function_name( [ parameter, ... ] );

The number of parameters a function requires differs from function to function (and, as we’ll see later, may even vary for the same function). The parameters supplied to the function may be any valid expression and must be in the specific order expected by the function. If the parameters are given out of order, the function may still run by a fluke, but it’s basically a case of “garbage in = garbage out.” A function’s documentation will tell ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming PHP, 3rd Edition

Programming PHP, 3rd Edition

Rasmus Lerdorf, Kevin Tatroe, Peter MacIntyre
Learning Java, 5th Edition

Learning Java, 5th Edition

Marc Loy, Patrick Niemeyer, Daniel Leuck

Publisher Resources

ISBN: 9781492054122Errata PageSupplemental Content