Home >Backend Development >PHP Tutorial >Functions in PHP
IN 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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
In PHP, mainly two functions are used by the programmers. They are:
These 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.
These 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.
Below are the points explain why should we use functions 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:
For String Functions
Code:
<!DOCTYPE html> <html> <body> <?php print_r(str_split("Hi This is a test sample")); ?> </body> </html>
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.
Code:
<!DOCTYPE html> <html> <body> <?php echo strcmp("Hi this is test","Hi this is test"); ?> <p>If this function returns 0, the two strings are same.</p> </body> </html>
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.
Code:
<!DOCTYPE html> <html> <body> <?php echo strpos("I love coding, I love php too!","coding"); ?> </body> </html>
Output:
The explanation for the above program: This function strpos() will check the position of the string that is passed as a parameter.
Code:
<!DOCTYPE html> <html> <body> <?php echo strrev("Hi world!"); ?> </body> </html>
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.
Code:
<!DOCTYPE html> <html> <body> <?php echo str_word_count("Hello this is the new world!"); ?> </body> </html>
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.
Code:
<!DOCTYPE html> <html> <body> <?php echo strlen("Hello this is the test sample!"); ?> </body> </html>
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.
For Numeric Functions
Code:
<!DOCTYPE html> <html> <body> <?php echo(abs(5.8) . "<br>"); echo(abs(-5.8) . "<br>"); echo(abs(-2) . "<br>"); echo(abs(3)); ?> </body> </html>
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.
Code:
<!DOCTYPE html> <html> <body> <?php echo(round(0.65) . "<br>"); echo(round(0.75) . "<br>"); echo(round(0.30) . "<br>"); ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php echo(sqrt(0) . "<br>"); echo(sqrt(7) . "<br>"); echo(sqrt(2) . "<br>"); echo(sqrt(0.45) . "<br>"); echo(sqrt(-3)); ?> </body> </html>
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.
Code:
<!DOCTYPE html> <html> <body> <?php // Check if the type of a variable is integer or not $x = 456; var_dump(is_int($x)); echo "<br>"; // Check whether the type of variable is integer or not $x = 66.58; var_dump(is_int($x)); ?> </body> </html>
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.
Code:
<!DOCTYPE html> <html> <body> <?php // Invalid calculation will return a NaN value $x = acos(10); var_dump($x); ?> </body> </html>
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.
Code:
<!DOCTYPE html> <html> <body> <?php $x = 11.35; var_dump(is_float($x)); ?> </body> </html>
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.
For User-defined functions
Code:
<!DOCTYPE html> <html> <body> <?php function Writefunction() { echo "Hello world!"; } Writefunction(); ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php function employee($ename) { echo "$ename Patil.<br>"; } employee("Akshay"); employee("Leela"); employee("Sharda"); employee("Subhadra"); employee("Akash"); ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php function Employee($ename, $id) { echo "employee name is $ename. Employee id is $id <br>"; } Employee("Heetal","778456"); Employee("Clark","567890"); Employee("Mohit","567894"); ?> </body> </html>
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.
Code:
<?php function addNumbers(int $a, int $b) { return $a + $b; } echo addNumbers(5, "13 days"); // since strict is NOT enabled "5 days" is changed to int(5), and it will return 10 ?>
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.
In 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.
The above is the detailed content of Functions in PHP. For more information, please follow other related articles on the PHP Chinese website!