© Mikael Olsson 2021
M. OlssonPHP 8 Quick Scripting Referencehttps://doi.org/10.1007/978-1-4842-6619-9_8

8. Functions

Mikael Olsson1  
(1)
Hammarland, Finland
 

Functions are reusable code blocks that only execute when called. They allow the code to be divided into smaller parts that are easier to understand and reuse.

Defining Functions

To create a function, the function keyword is used, followed by a name, a set of parentheses, and a code block. The naming convention1 for functions is the same as for variables—to use a descriptive name with each word initially capitalized, except for the first one.
function myFunc()
{
  echo 'Hello World';
}

A function code block can contain any valid PHP code, including other function definitions.

Calling Functions

Once defined, ...

Get PHP 8 Quick Scripting Reference: A Pocket Guide to PHP Web Scripting 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.