PHP Functions Example

Example of a Built-in Function

The length of the string "Hello, World!" is: 13 characters.

Example of a User-defined Function

The area of a rectangle with length 10 and width 5 is: 50.

Explanation of Functions

In PHP, functions are blocks of code that can be reused throughout a script. They allow you to encapsulate specific tasks or calculations, making your code more organized and easier to maintain. Functions can be built-in, which are provided by PHP (like strlen() for getting the length of a string), or user-defined, allowing developers to create their own functionality as needed.

To define a function, you use the function keyword followed by the function name and parentheses. Inside the parentheses, you can define parameters that the function can accept. Once the function is defined, you can call it by using its name and passing the required arguments.