Show Introduction to Functions in PHPIN PHP, many functions are used such as built-in functions and user-defined functions. Each and every function has its own functionality and properties. A function is a set of statements written in the program that can be used multiple times in the code anywhere needed. A function call is required to execute the statements written inside the function. It is a piece of code that takes one or more inputs as a parameter and processes it and returns a value. Programmers simply have to create a function and then call that function in the program wherever required. Types of Functions in PHPIn PHP, mainly two functions are used by the programmers. They are: 1. User-DefinedThese functions are used when the developer or programmer has to execute their own logic of code. These functions are defined using the keyword function and inside the function, a set of statements will be written to execute it when a function call occurs. The function call can be made by just simply calling the function like functionname(), and the function will get executed. 2. Built-inThese functions provide us with built-in library functions. PHP provides these functions in the installation package itself which makes this language more powerful and useful. To use the properties of the function we just need to call the function wherever required to fetch the desired result. There are many built-in functions used in PHP such as Date, Numeric, String, etc.
Why we should Use Functions in PHP?Below are the points explain why should we use functions in php:
How Functions are Used in PHP?As we discussed earlier, in PHP we have two functions i.e. built-in and user-defined. Let’s understand more about these functions: Example #1For String Functions Code:
Output: The explanation for the above program: In the above example, the string that we passed inside the function str_split(), splits the string on to a single character and produces the output. Example #2Code:
Output: The explanation for the above program: 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. Example #3Code:
Output: The explanation for the above program: This function strpos() will check the position of the string that is passed as a parameter. Example #4Code:
Output: The explanation for the above program: In the above example, the function strrev() will reverse the string passed as a parameter and provides the desired output. Example #5Code:
Output: The explanation for the above program: In the above example, the str_word_count() function will count the number of strings passed as a parameter and provides the desired output. Example #6Code:
Output: The explanation for the above program: In the above example, the strlen() function will count the number of characters present in the string and provides the count as the desired output. Example #1For Numeric Functions Code:
Output: The explanation for the above program: In the above example, the numeric function abs() will provide us the absolute value of the number that is passed as a parameter to the function. Example #2Code:
Output: Example #3Code:
Output: The explanation for the above program: In the above example, the parameters passed to the function sqrt() fetches the result by calculating the square root of the number and produces the desired output. Example #4Code:
Output: The explanation for the above program: In the above example, the var_dump() function will check the data type of a particular number passed as a parameter. In the above screenshot, the output is printed as true or false in the condition that the number should be an integer. If the number is not an integer it will return false else true. Example #5Code:
Output: The explanation for the above program: In the above example, the function var_dump() will check the datatype of the number passed as a parameter. In this example, the function acos() cannot calculate the number specified as a parameter and hence produces the output NAN which means that the calculation is incorrect. Example #6Code:
Output: The explanation for the above program: In the above example, the function is_float() will check whether the number passed as a parameter is of float datatype. This function always returns a Boolean value. If the result is positive then it will return true and if the result is negative it will return false. Example #1For User-defined functions Code:
Output: Example #2Code:
Output: Example #3Code:
Output: The explanation for the above program: In the above example, the employee names along with the employee id’s can be displayed by just calling the function employee () where the user wants to print the employee details. This user-defined functions can be used when the organization has a huge data and has to print all the employee details all together at a single go. Example #4Code:
Output: The explanation for the above program: In the above example, we have seen that the user-defined functions have their own properties and also the user can give his own inputs to get the desired output. User-defined functions are used by a programmer or developer to make his own changes in the code rather than use built-in functions. The main motive of using this function type is that the developer can make his own logic such as calculation of area of the circle, measurement of height, employee details, etc. PHP has loosely typed language where the datatypes are not set in a strict way, we can add the integer and string datatype values to fetch the output. In the example above the integer and string “5 and 13” are added together and the output is fetched as 18. This feature makes an advantage to the user. ConclusionIn this article, we discussed the types of functions in PHP and also its characteristics. The developers and programmers try to develop the code using these two functions as they don’t have to write it again and also the code are easy to test as it is written based on the type of task it has to perform. Recommended ArticlesThis is a guide to Functions in PHP. Here we discuss the types of function in php and how to use the function in php with the different examples. You can also go through our other related articles to learn more-
What are library functions in PHP?There are two types of functions - library functions and user functions. Library functions, such as array_push are part of the PHP library and can be used by anyone. However, you may write your own functions and use them across your code. A function receives a list of arguments separated by commas.
What are the different types of functions in PHP?PHP provides us with two major types of functions:. Built-in functions : PHP provides us with huge collection of built-in library functions. ... . User Defined Functions : Apart from the built-in functions, PHP allows us to create our own customised functions called the user-defined functions.. How many PHP functions are there?PHP has more than 1000 built-in functions, and in addition you can create your own custom functions.
What is PHP function with examples?PHP functions are similar to other programming languages. A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value. You already have seen many functions like fopen() and fread() etc.
|