PHP Functions

PHP Functions

PHP Functions


A function is a particular piece of code, which does some processing, and can be accessed from anywhere within the program.

There are already more than 1000 built-in library functions created for a different area, but we can create our own functions.

While creating a function its name should start with keyword function and all the PHP code should be put inside { and } characters.

Syntax of function:

function functionName() 
{
   // code to be executed;
}

Some features of user-defined functions in PHP

  • A function name can start with a letter or underscore (not a number)
  • A function name is not case-sensitive, that is to say function name() and function NAME() are the same.
  • A function will not execute immediately when a page loads.
  • A function will be executed by a call to the function.

Information can be passed to the functions via the argument list, which is a comma-delimited list of expressions.

PHP supports passing arguments by value, passing by reference, and default argument values.

function functionName($value, &$byReference, $default = 0) 

{

   // code to be executed;

}

By default, all arguments are passing by value. That is, if you change the value in a function, the value out of function will remain the same.

If you wish to allow a function to modify its arguments, you must pass them by reference.

<!DOCTYPE html>

<html>

<body>

<?php

   $byValue = 10 ;

   $byReferance = 10 ;

   // function call
   myFirstFunction($byValue, &$byReference);

   echo "byValue out of function is " . $byValue ;

   echo "byReference out of function is"  . $byReference ;


function myFirstFunction($byValue, &$byReferance) 

{

   $byValue = 20 ;

   $byReferance = 20 ;

   echo "byValue in function is" . $byValue ;

   echo "byReferance in function is" . $byReferance;

}

?>

</body>
</html>

As a result, we get:

byValue in function is 20
byReference in function is 20

by value out of function is 10
by reference out of function is  20

In Php, the function can define default values.

<!DOCTYPE html>

<html>

<body>

<?php

   myFirstFunction();

   // output 10 (by default function argument get 10 value)

   myFirstFunction(null);

   // output null (we pass new argument to function)

   myFirstFunction(20);

    // output 20 (we pass new argument to function)


function myFirstFunction($byDefault = 10) 

{

   echo $byDefault;

}

?>

</body>
</html>

 Note:

  The default value must be a constant expression.

   All arguments which are set to the default must be on the right side, for what the default values are not set.

Functions can return a value. For return value, used the return keyword. The returned value can be in any type.

<?php

 $x = myFirstFunction();

 echo $x;

 // output 10

function myFirstFunction() 
{
   return 10 ;
}

?>

Return ends the execution of the function and the control back to the line of code where the function was called so that we can use only one return in one function!!

<?php

$x = myFirstFunction();

echo $x;

// output 10
function myFirstFunction() 
{
   return 10 ;
   return 20;
   // This code will never work because the first return completes the function 
}

?>

Recommendation:

  Try to select a function name that describes your function.

  Try to not write duplicate codes, use function for all duplicate codes.

About most using PHP functions you will know in our next lessons.

Reactions

Post a Comment

0 Comments

close