PHP Functions

    • A function is a block of code that performs one or more particular tasks.
    • There are two types of functions in PHP as given below:

      1. Built in functions- These functions are already defined in php by php developers. Php is very rich in this matter. 2. User defined functions- Besides built in functions, a user can create a function according to the requirement.

      What is the need to create a function?



    • While writing a program, there may be some part of code which we want to be executed many times at different places. Therefore, writing the code at all the places will make the program longer, annoying and more complicated.
    • There comes the use of a function. We can create a function of the code that we want to be executed many times. Thus we have to write the code only once and now we just have to call the function wherever we want that code.


    Syntax and Call



  • A function is created using the keyword function.

  • The name of the function is always followed by a parenthesis (brackets).
  • syntax



    function functionname()

    {

    code

    }
    • This is called the function definition.
    • To execute the code of the function, we must call the function by its name. A function is never executed until it is called from somewhere in the program.
    • After the execution, the control again returns to the place where the function was called from and resumes the program from there.
    • <!DOCTYPE html>

      <html>

      <body>

      <?php

      function first()

      {

      echo "We are inside the function<br>";

      }

      echo "We are outside the function<br>";

      first(); //call the function

      echo "We are again outside the function"

      ?>

      </body>

      </html>

      Output



    • A function is defined whose name is first(). But when the file is run, this function is not executed automatically. The echo statement which is written after the function has been executed first.
    • After this echo statement, we have called the function by its name. As soon as the function is called, its code has been executed.
    • When the function code is executed entirely, the control returns back to the place from where the function was called and executes the next statement written just after the function call.

    Passing Arguments



    • You can also pass one or more values to the functions.
    • The passed values are called the arguments.
    • The arguments that are to be passed to the function, are written in the parenthesis at the time of function call.
    • Multiple arguments are separated using commas.
    • Every argument is received by the function in a different variable which is mentioned in the parenthesis in function definition.
    • If a variable is passed to a function, it will be received in another variable. Now if we make change in the variable in function, the original variable will remain unchanged.
    • Following example will clear the above points-

      <!DOCTYPE html>

      <html>

      <body>

      <?php

      function first($str,$var)

      {

      echo "$str the value is $var<br>";

      $var++;

      echo "Changed variable is $var<br>";

      }

      $a=5;

      first("Hello ",$a);

      echo "The original variable is $a";

      ?>

      </body>

      </html>

      Output





    • Two arguments have been passed when the function is called; a string "Hello" and a variable a. These arguemnts are received in the function definition in the variables str and var respectively.

    Returning a value



    • When the code of the function is over, the control comes back to the point where the function was called from.
    • You can make it to carry some value with it. In other words, a function can return a value.
    • return statement is used in a function to make it return a value.
    • A function can return only one value.
    • You must place a variable to receive this returned value as-
    • $var=functionname();

      <!DOCTYPE html>

      <html>

      <body>

      <?php

      function first($x,$y)

      {

      $sum=$x+$y;

      return $sum;

      }

      $a=first(10,15);

      echo "The returned value is $a";

      ?>

      </body>

      </html>

      Output





    • The statement $a=first(10,15); tells that we are calling a function first() with passing two arguments 10 and 15 and this fucntion will return some value which will saved in variable a.
    • In fucntion definition value of variable sum is returned using return statement which has been saved in variable a.