Functions - PHP Programming: Learn PHP Programming - CRUSH IT IN ONE DAY. Learn It Fast. Learn It Once. Get Coding Today, First Edition(2015)

PHP Programming: Learn PHP Programming - CRUSH IT IN ONE DAY. Learn It Fast. Learn It Once. Get Coding Today, First Edition (2015)

Chapter 6. Functions

In any scripting or programming language, a function is nothing but a block of statements which can be used repeatedly in a program. Even in PHP functions are the same, like any other programming or scripting language. In the standard PHP distribution there are more than 1000 built-in functions.

User defined functions:

Apart from the built-in functions from the standard PHP distribution, the user can define functions as per his own requirements. These types of functions are called as user defined functions.

Syntax: The syntax for user-defined functions is given below. Using the syntax you can define your own functions and can use them anywhere you desire.

function function-name()
{
statement 1 :
statement 2 :
statement 3 :
......
}
Elements of a Function

· Function: The special word 'function' is used in front of a function for declaring a function.

· Name of the function: The name of the functions can be defined by the user. Function names are case sensitive. So you should be careful when using a function. A functional name should start with a letter or an underscore. This can be followed by any number of numbers, letters or underscores.

· Opening and closing curly braces ( { } ) : The body and code of a function should be enclosed within opening and closing curly braces. The opening curly brace ( { ) is considered the beginning of the function and the closing curly brace ( } ) indicates the end or termination of a function.

Example: Here is an example of a PHP function.

<?php

function myfunction()

{

echo “hello world”;

}

myfunction();

?>

When the function is called, it will print the words ‘hello world’.

Functions within functions

We can place functions within other functions. Here is an example showing a function within a function.

<?php

function function1()

{

function function2()

{

echo "Hey There!! <br>";

}

}

function1();

function2();

?>

Here a function, function1() is declared. Inside the function1(), another function, function2() is declared. Now if the function1() is executed, it will execute the function2() within it and will print "Hey There!!". Function2() is made accessible by executing the function1(). The function2() cannot be executed independently without executing the function1().

PHP function arguments, returning values

Function arguments

In PHP, argument lists are used to pass information to the functions. It is a comma-delimited list of expression. Passing arguments to a function can be done in 3 different ways.

Example 1:

<?php

function familyName($fname) {

echo "$fname Refsnes.<br>";

}

familyName("Thor");

familyName("Stark");

familyName("Hawk");

familyName("Banner");

familyName("Rogers");

?>

Example 2: Function with two arguments.

<?php

function familyName($fname, $year) {

echo "$fname Refsnes. Born in $year <br>";

}

familyName("Wayne", "1975");

familyName("Kent", "1978");

familyName("Allen", "1983");

?>

Arguments by Value (default): any valid expression can be an argument. In the function, the expression will be evaluated and the value time will be assigned to the appropriate variable. In the following example, $a is assigned a value 5 and $b is assigned 10.

function add($a, $b)

{

........

}

add(5,10);

Passing arguments by reference: function arguments, by default are passed by value. You will need to pass the arguments by difference if you want to lower the given function to change the arguments. By adding an ampersand (&) in front of the variable name, you can pass the arguments by reference to a function.

Example :

<?php

function cube(&$x)

{

$x = $x* $x * $x;

}

$result = 4;

cube($result);

echo $result;

?>

This will generate an output of 64.

Default argument values: The default value will be taken as argument when the function is called without arguments. Here is an example which uses the default argument.

Example 1:

<?php

function wage($minwage= 100)

{

echo "The wage is : $minwage <br />" ;

}<

wage(200);

wage();

wage(100);

?>

Output:

The wage is : 200

The wage is: 100

The wage is: 100

Example 2:

<?php

function setHeight($minheight = 50) {

echo "The height is : $minheight <br>";

}

setHeight(350);

setHeight();

setHeight(135);

setHeight(80);

?>

Here in this example, the default value of 50 will be taken.

Output:

The height is : 350

The height is : 50

The height is : 135

The height is : 80

PHP: returning values

The return statement written is the values in PHP. In the variable, the return values should be specified. The function will be terminated immediately if the statement is called from that function. The control will be passed back to the previous position from where it was called. All types of data can be returned by the return statement.

Example 1:

<?php

function cube($x)

{

return $x * $x * $x;

}

echo "The cube of 4 is : ".cube(4)."<br />";

echo "The cube of 9 is : ".cube(9)."<br />";

echo "The cube of 20 is : ".cube(20)."<br />";

?>

Output:

The cube of 4 is : 64

The cube of 9 is: 729

The cube of 20 is: 8000

Example 2:

<?php

function sum($x, $y) {

$z = $x + $y;

return $z;

}

echo "5 + 10 = " . sum(5, 10) . "<br>";

echo "7 + 13 = " . sum(7, 13) . "<br>";

echo "2 + 4 = " . sum(2, 4);

?>

Output:

15

20

6

Variable functions

The concept of variable functions is supported in PHP. If a parenthesis is appended to a variable name, PHP looks for a function which has the same name as the variable evaluating it, which will then execute it. With variable function syntax, object methods can also be called.

Example 1: variable function example.

<?php

function foo() {

echo "In foo()<br />\n";

}

function bar($arg = '')

{

echo "In bar(); argument was '$arg'.<br />\n";

}

// This is a wrapper function around echo

function echoit($string)

{

echo $string;

}

$func = 'foo';

$func(); //This calls foo()

$func = 'bar';

$func('test'); // This calls bar()

$func = 'echoit';

$func('test'); // This calls echoit()

?>

Example 2: variable method example

<?php

class Foo

{

function Variable()

{

$name = 'Bar';

$this->$name(); //This calls the Bar() method

}

function Bar()

{

echo "This is Bar";

}

}

$foo = new Foo();

$funcname = "Variable";

$foo->$funcname(); // This calls $foo->Variable()

?>