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:

Note: Prerequisite: Basic understanding of types

Although PHP is a loosely typed language, it's important to have a basic understanding of types as they have important meaning.

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.

Explanation of a function definition
Part Description
strlen The function name.
(PHP 4, PHP 5, PHP 7) strlen() has been around in all versions of PHP 4, 5 and 7
( string $string ) The first (and in this case the only) parameter/argument for this function is named string, and it's a string.
int Type of value this function returns, which is an int (i.e. the length of a string is measured in numbers).

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, true on success (if the needle was found in the haystack) or false on failure (if the needle was not found in the haystack). The first parameter is named needle and it can be of many different types, so we call it "mixed". This mixed needle (what we're looking for) can be either a scalar value (string, integer, or float), or an array. haystack (the array we're searching in) is the second parameter. The third optional parameter is named strict. All optional parameters have default values; if the default value is unknown, it is shown as ?. The manual states that the strict parameter defaults to boolean false. See the manual page on each function for details on how they work.

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 &$matches will be passed as reference.

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.

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Definition of stored procedures - an introduction

Stored procedures are predefined SQL code blocks that are stored in a database and can be called up as required. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial