This post covers the Syntax section of the Functions chapter when studying for the Zend PHP 7 Certification.
Functions can be best described as packages of code that serve as instructions to execute an action. These packages are often re-used in projects to avoid code duplication.
Any valid PHP code can reside in a function, and to define one, you can use the function
keyword like the example below.
<?php
function foo ($arg_1) {
return 'Some return value';
}
Functions follow the same naming conventions as variables in PHP, in that they can start with a letter or underscore, followed by any number of letters, numbers, or underscores. Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration. However note that you cannot redefine a function.
Functions do not need to be defined before they are referenced, unless a scenario occurs when a function is conditionally defined.
<?php
$makefoo = true;
/* We can't call foo() from here
since it doesn't exist yet,
but we can call bar() */
bar();
if ($makefoo) {
function foo() {
echo "I don't exist until program execution reaches me.\n";
}
}
/* Now we can safely call foo()
since $makefoo evaluated to true */
if ($makefoo) foo();
function bar() {
echo "I exist immediately upon program start.\n";
}
This outputs:
I exist immediately upon program start.
I don't exist until program execution reaches me.
Parameters and return values within functions are optional, however it is advised to set a default parameter to avoid generating a PHP warning like the following.
<?php
function myFunction($p) {
return $p;
}
$x = myFunction("ABC"); // x = "ABC"
$x = myFunction(); // Warning
function myOtherFunction($p = "ABC") {
return $p;
}
$x = myOtherFunction("DEF"); // x = "DEF"
$x = myOtherFunction(); // x = "ABC"
All functions and classes in PHP have the global scope, in that they can be called outside a function even if they were defined inside and vice versa.
PHP comes standard with many in-built functions and constructs. There are also functions that require specific PHP extensions compiled in, otherwise fatal “undefined function” errors will appear. For example, the mysqli_connect()
function can be used to connect to a database.
Use the phpinfo()
or get_loaded_extensions()
function which will show the extensions that are loaded into PHP.
Since PHP version 7.1, new void functions and methods are possible. Functions declared with void
as their return type must either omit their return statement altogether, or use an empty return statement. NULL
is not a valid return value for a void function.
<?php
function should_return_nothing(): void {
return 1; // Fatal error: A void function must not return a value
}
function returns_null(): void {
return null; // Fatal error: A void function must not return a value
}
function lacks_return(): void {
// valid
}
function returns_nothing(): void {
return; // valid
}
View the other sections:
Note: This article is based on PHP version 7.1.