i

PHP Tutorial

Defining A function

To define a function or the piece of reusable code, we need to wrap the function inside curly brackets {} to represent the start and end of the function code respectively. Function arguments or parameters are written under function definition after giving the function construct and function name. Parameters have to be written in round brackets().

The arguments we create in the function definition are similar to variables as when we call the function, and the values are passed to these parameters or arguments. These are future placeholders for the values. Definition of function is as below:

Function name ($arg1, $arg2)

{

//Code that needs to be executed;

We can call the function by using the function name and passing the values to the parameters as below:

Name($val1, $val2);

We can also define a function without parameter as below example.

 function greeting()

{

           echo “Welcome to the PHP course”;

}

greeting();

?>

We have created a reusable echo statement that we can call as many times as required.