Functions in PHP with Examples. In PHP, several functions are used like, built-in functions and user-defined functions. Each and all function has its individual functions and properties. A function is a number of statements addressed in the program that can be utilized many times in the code wherever required. A function request is needed to run the statements composed inside the function. It is a part of code that needs one or more extra data as an input parameter and processes it and declares a value. Programmers completely have to build a function and then call that function in the program anywhere required.
Types of Functions in PHP
In PHP, essentially two functions are managed by the programmers. They are:
- Built-in Functions.
- User-defined Functions.
Built-in Functions in PHP
These functions give us built-in library functions. PHP gives these functions in the installation set itself which performs this language more effectual and helpful. To use the properties of the function we just require to request the function anywhere needed to fetch the wanted result.
There are many built-in functions used in PHP such as Date, Numeric, String, etc.
- String Functions: These functions have a predefined meaning and functionality in PHP to work with strings. PHP has many string functions such as strpos(), strncmp(), strrev(), strlen(),
- Date Function: These functions are predefined meaning and functionality in PHP where the format is a UNIX date and time which is a human-readable format.
- Numeric Functions: These functions have their own predefined meaning, functions, and logic presented by PHP which is used for numeric operations. It will deliver the result either in Boolean form or in numeric form. Some of the numeric functions include is_number(), number_format(), round() ,etc.
User-Defined Functions in PHP
These functions are done when the programmer or developer has to run their own logic of the program. These functions are specified using the keyword function and inside the function, a set of statements will be composed to run it when a function call happens. The function call can be performed by just simply calling the function like function_name(), and the function will get performed.
How Functions are Used in PHP?
As we explained beginning, in PHP we have 2 functions e.g. built-in and user-defined. Let’s explain more about these functions:
Built-in Code Example
<?php echo strcmp("Hello It is a test text","Hello It is a test text"); ?> <p>If this function returns 0. So, the two strings are same.</p>
The description for the above Code: In the above example, the function strcmp () will compare the strings and if the strings are the same it will return zero and if the strings are not equal then it will return some other number.
If this function returns 0, the two strings are same.
User-Defined Code Example:
<?php function addIntegers(int $x, int $y) { return $x + $y; } echo addIntegers(10, "12 days"); // since stern is NOT allowed "10 days" is modified to int(10), and it will return 22 ?>
The description for the above Code: In the above example, we have understood that the user-defined functions have their own characteristics, and also the user can provide his own inputs to get the wanted output. User-defined functions are managed by a programmer or developer to create his own modifications in the code preferably than use built-in functions. The central purpose of using this function type is that the developer can create his own logic such as calculation of the area of the circle, the measure of height, employee details, etc.
PHP has a loosely typed language where the data types are not set in a strict way, we can add the integer and string data type values to fetch the output. In the example above the integer and string ’10 and 12′ are added together and the output is obtained as 22. This feature creates an interest in the user Functions in PHP.
Conclusion
In this chapter, we explained the standards of functions in PHP and also its characteristics. The programmers and developers try to develop the program using these two functions as they don’t have to compose it again and also the program is simple to test as it is composed based on the type of work it has to complete.