A function is a unit of code that represents a sequence of statements. Functions can accept arguments or values and return a single value (or not).
Sample Code<?php
/* Function That Returns Age Based On Provided Birth Year
The return type is string.
The name of the function is myAge
The number of arguments to the function is 1:
The first argument is of type int.
*/
function myAge(int $birthYear): string
{
// calculate the age by subtracting the birth year from the current year.
$yearsOld = date('Y') - $birthYear;
// return the age in a descriptive string.
return $yearsOld . ($yearsOld == 1 ? ' year' : ' years');
}
echo 'I am currently ' . myAge(1995) . ' old.';
?>
While loop executes a set of statements as long as a condition is true. It requires relevant variables to be ready such as an indexing variable to iterate through a loop.
The for loop is C-style in that it requires three parts, the initialization or loop variant, the condition and advancement to next iteration. It is used to iterate over data when referring to a changing index.
During the execution of a program, it may be required to process special conditions called exceptions. An exception breaks the normal execution flow and executes a specified exception handler.
An exceptional event could occur from an error and the PHP will try to find a matching exception block. PHP uses try with at least one corresponding catch or finally block. An exception can be thrown or caught.