PHP have two types funcation available :
1 .PHP Built-in Functions
PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task like gettype(), print_r(), var_dump, etc.
2 .PHP User-Defined Functions
PHP user-defined function declaration starts with the function keyword :
Creating PHP Function
Its very easy to create your PHP function. Create a PHP function which will write a simple message on your browser when you will call it function. Following example creates a function called currentDate() and then calls it just after creating it.
<?php function currentDate(){ echo "Today Date is " . date("d-m-Y"); } currentDate(); ?>
PHP Functions with Parameters
In this function you can call function with pass arguments parameters.
<?php function sum($num1, $num2) { $sum = $num1 + $num2; echo "Sum of the two numbers is : $sum"; } sum(10, 20); ?>
PHP Functions returning value
This function can return a value using the return statement with a value or object.
If sometime you want to return a value if you are using functions. This is where the return statement comes in. Let’s look at an example:
<?php function sum($num1, $num2) { $sum = $num1 + $num2; return $sum; } $total = sum(10, 20); echo "Total is : $total"; ?>
Set Default Values for Function Parameters
In this function you can not pass any value at that this function set default value.
<?php function defaultfunction($no = 20) { print $no."<br />"; } defaultfunction(10); defaultfunction(); ?>