How to read a function definition (prototype)Each function in the manual is documented for quick reference. Knowing how to read and understand the text will make learning PHP much easier. Rather than relying on examples or cut/paste, everyone should know how to read function definitions (prototypes). Let's begin:
Function definitions tell us what type of value is returned. Let's use the definition for strlen as our first example:
strlen (PHP 4, PHP 5, PHP 7) strlen -- Get string length Description strlen ( string $string ) : int Returns the length of given string.
We could rewrite the above function definition in a generic way:
function name ( parameter type parameter name ) : returned type Many functions take on multiple parameters, such as in_array. Its prototype is as follows:
in_array ( mixed $needle, array $haystack , bool $strict = false ) : bool
What does this mean? in_array() returns a
boolean value, In addition the & (ampersand) symbol prepended to a function parameter allows the parameter to be passed by reference, as seen below:
preg_match ( string $pattern , string $subject , array &$matches = null, int $flags = 0 , int $offset = 0 ) : int|false
In this example, we can see the third optional parameter There are also functions with more complex PHP version information. Take html_entity_decode as an example:
(PHP 4 >= 4.3.0, PHP 5, PHP 7) This means that this function has only been available in a released version since PHP 4.3.0. |