Creating and Calling Your Own Functions

As you’ve already seen, PHP has a lot of built-in functions for almost every need. More importantly, though, it has the capability for you to define and use your own functions for whatever specific purpose. The syntax for making your own function is

function function_name () {
   // Function code.
}

The name of your function can be any combination of letters, numbers, and the underscore, but it must begin with either a letter or the underscore. In PHP, as I mentioned in the first chapter, function names are case-insensitive (unlike variable names) so you could call that function using function_name() or FUNCTION_NAME() or function_Name(), etc.

The code within the function can do nearly anything, from generating ...

Get PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide 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.