Functions in PHP

What is a Function?
      Function is a independent set of code statements, written to perform some tasks.

Function may or may not accept / return a value.

Function names are case-insensitive in PHP.

In PHP there are mainly 2 types of Function.

1) built-in (ready to use)
2) user-defined (created by user)

Why to use a Function?

Functions are written mainly because of reusability.(Write once and use forever)

Functions are easy to manage, simple to read.

How to use a Function?

function uniquename (arguments)
//some code
return value/variable; // optional return statement

The uniquename can be any string that starts with a letter or underscore.

Function Arguments / Parameters:

Depending on type, Parameters to a function can be passed in 2 ways.

Call by Value:

copy of variables are passed as parameters. hence original value is not changed.

function addthem( $n1, $n2)
   $sum = $n1 + $n2;
  return $sum;

 echo " Addition = " .addthem(123,321);
Call by Reference:

Address of variables are passed as parameters, hence original value may be changed.

function swapthem(&$n1,&$n2) 
echo "Before swap : a=".$a ." and  b = $b<br/>";
echo "After swap : a=".$a ."  and  b = $b<br/>";

No comments:

Post a Comment

Thanks for visiting my Blog!

Have a Question ? Need Help in College Assignments, Need Code that is not available here? Just leave a comment & get your code instantly.

Tips to Enhance Your Blog