Searching...
Wednesday 26 June 2013

Functions in php – Part 1


function
You’ll often find that some tasks are comprised of bits of logic which can be reused elsewhere, not only within the same application but also in many other applications. For example, an e-commerce application might need to validate an email address on several different pages, such as when a new user registers to use a Web site or when a visitor signs up for a newsletter. The logic used to validate an e-mail address is quite complex, and therefore it would be a smart act to maintain the logic in a single location rather than literally embed  it into numerous pages, particularly if it one day needs to be modified to account for a new domain.

The concept of embodying these repetitive processes within a named section of code and then invoking this name when necessary has long been a key feature of modern computer languages which is known as a Function, and it grants you the convenience of a singular point of reference if the process it defines requires changes in the future, which greatly reduces both the possibility of programming errors and maintenance overhead.

In this series of lecture, we'll be covering each and every aspect of a Function, including how to create and invoke them, pass input to them(by value and by reference), use a relatively new feature known as type hinting, return both single and multiple values to the caller, and create and include function libraries. Additionally, you’ll learn about both recursive and variable functions.
Note:- Keep an eye on each and every aspect of function coz it's quite important.

Invoking a Function

More than 1,000 functions are built into the standard PHP distribution and you can include any function defined elsewhere by include() or require(). You can anytime invoke any of these functions by just specifying their names. For example, to raise five to the third power:-

<?php
$value = pow(5,3); // returns 125
echo $value;
?>

Note:- pow is predefined library function termed as power. pow(x,y) will raise x to the y power

In the example above, you can bypass assigning the value to a variable and simply get an output, like this:

<?php
echo pow(5,3);
?>

Output:
125

If you want to output the function outcome within a larger string, you can do this as :-

<?php
printf("Five raised to the power three is %d.", pow(5,3));
?>

Output:
Five raised to the power three is 125.

Note:- Refer to the php documentation for complete list of predefined functions.

Creating a Function

Although php offers a vast variety of predefined functions but sooner or later you will feel the need to go beyond these predefined functions and create custom functions or even entire function libraries.
You can easily create a function as follows:

function functionName(parameters)
{
function-body
}

Take an example:-

function helloworld()
{
echo "Hello world!";
}

Once defined you can call this function as follows:

<?php
helloworld();
?>

Output:
Hello world!

Note:- In the example above there are no parameters. Parameters are like variables in the function and there could be none or any number of parameters inside a function.
Now take this example:-

<html>
<head>
<title>PHP Function with Parameters</title>
</head>
<body>
<?php
function addFunction($num1, $num2)
{
    $sum = $num1 + $num2;
    echo "Sum of the two numbers is : $sum";
}
addFunction(10, 20);
?>
</body>
</html>

Output:
function parameters

Most of the times it is not necessary to define a function before it is called except when it is conditionally defined. This is because of the fact that php looks over the entire script into engine before executing it. BUT THIS IS HAPHAZARD PRACTICE IS NOT RECOMMENDED. SO DON'T TRY THIS.

Take this as example:-

<?php
$makefoo = true;
/* We can't call function foo() from here
   since it doesn't exist yet,
   but we can call bar() */
bar();
if ($makefoo) {
  function foo()
  {
    echo "I don't exist until program execution reaches me.\n";
  }
}
/* Now we can safely call foo()
   since $makefoo evaluated to true */
if ($makefoo) foo();
function bar()
{
  echo "I exist immediately upon program start.\n";
}
?>

Function within function:-


<?php
function foo()
{
  function bar()
  {
    echo "I don't exist until foo() is called.\n";
  }
}
/* We can't call bar() yet
   since it doesn't exist. */
foo();
/* Now we can call bar(),
   foo()'s processing has
   made it accessible. */
bar();
?>

0 comments:

Post a Comment

 
Back to top!