Tuesday, January 9, 2018

User define function in PHP


A function is a named block of code that performs a specific task, possibly acting upon a set of values given to it, or parameters , and possibly returning a single value. Functions save on compile time—no matter how many times  call them, functions are compiled only once for the page. They also improve reliability by allowing you to fix any bugs in one place, rather than everywhere  perform a task, and they improve readability by isolating code that performs specific tasks.
Free Search Engine Submission

The syntax of function calls and function definitions and discusses how to manage variables in functions and pass values to functions (including pass-by-value and pass-by-reference). It also covers variable functions and anonymous functions.

Some example of function:- 

Define a function:- 

To define a function, use the following syntax: 

function      [&] 

function_name ([ parameter [, ...]]) 

statement list 

The statement list can include HTML. You can declare a PHP function that doesn’t contain any PHP code. For instance, the column() function simply gives a convenient short name to HTML code that may be needed many times throughout the page: 

<? php function column () 

{ ?> 




The Function names are case-insensitive; that is, you can call the sin() function as sin(1) , SIN(1) , SiN(1) , and so on, because all these names refer to the same function. By convention, built-in PHP functions are called with all lowercase. 

    Typically, functions return some value. To return a value from a function, use the return statement: put return expr inside  function. When a return statement is encountered during execution, control reverts to the calling statement, and the evaluated results of expr will be returned as the value of the function. 

We can include any number of return statements in a function (for example, if  have a switch statement to determine which of several values to return).

Let’s take a look at a simple function. Example  takes two strings, concatenates them, and then returns the result.


function strcat ( $left , $right ) 

$combinedString = $left . $right ; 

return $combinedStri


The function takes two arguments, $left and $right . Using the concatenation operator, the function creates a combined string in the variable $combinedString . Finally, in order to cause the function to have a value when it’s evaluated with our arguments, we return the value $combinedString . 
Because the return statement can accept any expression, even complex ones, specify Programming in mansion.

 function strcat ( $left , $right ) 

return $left . $right ; 


If we put this function on a PHP page, we can call it from anywhere
within the page. Take a look at Example. 


<? php 

function strcat ( $left , $right ) 

return $left . $right ; 

$first = "This is a " ; 

$second = " complete sentence!" ; 

echo strcat ( $first , $second );

We can nest function declarations, but with limited effect. Nested declarations do not limit the visibility of the inner-defined function, which may be called from anywhere in your program.

The inner function does not automatically get the outer function’s arguments. And, finally, the inner function cannot be called until the outer function has been called, and also cannot be called from code parsed after the outer function. Read more 

They are shown  in image understanding by functions:-


Please comment and follow this site blog inbox