In this tutorial you will learn about the PHP Functions and its application with practical example.
PHP functions are block of code defined to perform a specific task. PHP functions are like other programming languages. A function is a block of code which can takes one more input in the form of arguments and perform some processing and may or may not returns a value.
Syntax for Creating function in PHP :
1 2 3 |
function <function_name>(<list of parameters>) { //Block of Statement } |
Example1:
1 2 3 4 5 6 7 8 9 10 |
<?php Function hello() { Echo "hello world"; } hello(); ?> Output: hello world |
Example2:
Function with one parameter
1 2 3 4 5 6 7 |
function plusOne($x) { $x = $x + 1; return $x; } echo $newval=plusOne(5);//function call Output: 6 |
Example2:
Function with parameter with default value
1 2 3 4 5 6 7 8 9 10 11 |
function printName($param = 'Tester') { print $param; } printName("User1"); printName(); ?> Output: User1 Tester |