Home PHP Ajax Regex Interviews Contact us    

PHP


Interview Questions



Functions

Functions are the tasks or actions that software is intended to perform.They are blocks of reusable code that can be passed parameters and can return a value. For example, the getAddress function is passed the name of a property and and it returns the address of that name. The getVersion function returns the version of the Flash Player currently playing the movie.

You can also find a huge list of predefined functions built into PHP in the PHP Manual's function reference


Declaring a function in PHP

The word "function" is a reserved keyword to tell the parser you are declaring a function.

The function is given a name (here func_name). You may use any name you wish that starts with a letter, and is not a reserved word or an existing function.

Functions often have parameters that operate similarly to variables.


<?php
function func_name(parameters)
{
//statements
}
?>

Functions with Parameters

Parameters are variables that exist only within that function. They are provided by the programmer when the function is called . When declaring or calling a function that has more than one parameter, you need to separate between different parameters with a comma ','.


<?php
function Two_Names($name1, $name2)
{
echo $name1;
echo "\n";
echo $name2;
return NULL;
}
?>

To call this function you must give the parameters a value. It doesn't matter what the value it, as long as there is one.

function call:

<?php
Two_Names("paul", "Joseph"); ?>

Output:

paul
Joseph

You can pass the values to the parameters when declaring a function , it will act as a default parameter value.

<?php
function Two_Names($name1 = "john", $name2 = "joseph")
{
echo($name1);
echo("\n");
echo($name2);
}
?>

function call:

<?php
Two_Names("hello", "Joseph");
?>

Output:

hello
Joseph

Functions with Returning a value

This function is all well and good, but usually you will want your function to return some information

To return a value from a function use the return() statement in the function.


<?php
function Findsum($var1 = 0, $var2 = 0, $var3 = 0)
{
$var4 = $var1 + $var2 + $var3;
return $var4;
}
?>

function call:

<?php
$sum = Findsum(10,20,30);
echo "sum = ".$sum;
?>

Output:

sum = 60


Feed Back of this Topic
 
Name :
Email :
Topic :
Comments :

Ajax


Regular Expression


 
Copyright © 2007 123developers.com
Contact us | Disclaimer