Functions In PHP


A function is a block of statements that can be used multiple times in the code. A function can be with or without any arguments or callbacks.

A function will not execute immediately when a page loads.

Functions are only executed if called in main code else not.

eg



<?php

// simple function with no arguments and callbacks
function ourcoaching() {
    echo "Learn PHP from ourcoaching.com";
}
ourcoaching(); // calling function

echo "<br>";

// function with an argument
function ourcoaching_args($x) {
    echo "value input is = ".$x;
}
ourcoaching_args(422); // calling function
echo "<br>";

// function with an argument and callbacks
function ourcoaching_callback($x) {
return $x*10;
}
$y=ourcoaching_callback(22); // calling function
echo "value of callback function is = ".$y
?>


Output is as follows:

Learn PHP from ourcoaching.com
value input is = 422
value of callback function is = 220




Related Tutorials