How many types of functions are in php?
The function is a self-contained block of statements that can repeatedly be executed whenever we need it. Show
The concept of the function is the same as in other languages like C. There are more than 1,000 inbuilt functions into the standard PHP distribution. Apart from these, we can define own functions according to the requirements. Syntax:
Types of Functions in PHP
Internal Built-in Functions in PHP
For a complete reference and examples of the predefine functions, please visit http://php.net/manual/en/funcref.php Some (not even most) usage of built-in functions are as mentioned below:
Using Your own PHP Function (User Defined Functions)
Example:
Example:
Returning ValuesAll functions in PHP return a value, even if you don't explicitly cause them to. Thus, the concept of "void" functions does not apply to PHP. You can specify the return value of your function by using the return keyword: Example:
Note, however, that even if you don't return a value, PHP will still cause your function to return NULL. PHP Function ArgumentsInformation can be passed to functions through arguments. The following example has a function with two arguments: Example:
PHP Default Argument ValueThe following example shows how to use a default parameter. If we call the function setWeight() without arguments it takes the default value as an argument: Example:
A function is a block of code written in a program to perform some specific task. We can relate functions in programs to employees in a office in real life for a better understanding of how functions work. Suppose the boss wants his employee to calculate the annual budget. So how will this process complete? The employee will take information about the statics from the boss, performs calculations and calculate the budget and shows the result to his boss. Functions works in a similar
manner. They take informations as parameter, executes a block of statements or perform operations on this parameters and returns the result.
Why should we use functions?
Creating a Function While creating a user defined function we need to keep few things in mind:
Syntax: function function_name(){ executable code; } Example: PHP
Output: This is Geeks for Geeks Function Parameters or Arguments The information or variable, within the function’s parenthesis, are called parameters. These are used to hold the values executable during runtime. A user is free to take in as many parameters as he wants, separated with a comma(,) operator. These parameters are used to accept inputs during runtime. While passing the values like during a function call, they are called
arguments. An argument is a value passed to a function and a parameter is used to hold those arguments. In common term, both parameter and argument mean the same. We need to keep in mind that for every parameter, we need to pass its corresponding argument. function function_name($first_parameter, $second_parameter) { executable code; } Example: PHP
Output: The product is 30 Setting Default Values for Function parameter PHP allows us to set default argument values for function parameters. If we do not pass any argument for a parameter with default value then PHP will use the default set value for this parameter in the function call. PHP
Output: Ram is 15 years old Adam is 12 years old In the above example, the parameter $num has a default value 12, if we do not pass any value for this parameter in a function call then this default value 12 will be considered.
Also the parameter $str has no default value , so it is compulsory. Returning Values from Functions Functions can also return values to the part of program from where it is called. The return keyword is used to return value back to the part of program, from where it was called. The returning value may be of any type including the arrays and objects. The return statement also marks the end of the function and stops the execution after that and returns
the value. PHP
Output: The product is 30 Parameter passing to Functions PHP allows us two ways in which an argument can be passed into a function:
Example: PHP
Output: The original value is still 10 The original value changes to 20 This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can
also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. What are 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.. What are all the functions in PHP?The table below shows the common PHP numeric functions. What are functions in PHP with example?A function is a piece of code that takes another input in the form of a parameter, processes it, and then returns a value. A PHP Function feature is a piece of code that can be used over and over again and accepts argument lists as input, and returns a value. PHP comes with thousands of built-in features.
What is the function name in PHP?Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$ . See also the Userland Naming Guide.
|