Functions - LEARNING PHP AND MYSQL (2015)

LEARNING PHP AND MYSQL (2015)

Functions

· Functions are groups of PHP statements grouped together that are or can be used over and over again.

· It has more than 1000 built in and user defined function.

· Function don’t execute immediately.

· Functions only execute when u call them.

· Statement may contain multiple functions.

User-defined function

It can be defined as shown in the example.

Example

<?php

Function foo ($arg_1, $arg_2, ……., $args_n )

Echo “ example.\n” ;

Return $retval ;

}

?>

Function argument

Through a argument list the information can be passed to the function. This is a comma delimited list of expression.

Example

<? php

Function takes_array ($input)

{

Echo “$input[0] +$input[1] = “,$input[0]+ $input[1];

}

?>

Returning values

Values are returned using the optional return statement. Any type can be returned, including arrays and objects.

Example

<? php

Function square ($num)

{

Return $num * $num;

}

Echo square (4);

?>

Internal (built-in) function

These are already set in the library. There also function that requires certain specified PHP extension compiled.

Example

To create image function we use imagecreatetruecolor ().

Anonymous function

This function is also known as closure, this has no specific name.

Example

<? php

Echo preg_replace_callback (‘~-([a-z]) ~’, function ($match [1]) ;

}, ‘hello-world’);

?>