PHP functions


php PHP functions Functions

PHP function and execution of PHP function

Basically, PHP function is the piece of code which takes one or more input values , this input value is in the form of parameter. Function work according to the code that is written in the body of the function. There may be some functions that works without input values. There are two following type of functions. 1- Buit-in functions 2- User defined functions Lets discuss these types of function briefly.

1-Built-in functions

Those functions that are predefined by programming language are called built-in functions. PHP is famous for its more than 1000 built-in functions. e.g print_f function

2- User defined functions

Those function that user defines them to meet for the specific need are called user defined functions. It is very easy to write the function in PHP .

2- User defined functions

Those function that user defines them to meet for the specific need are called user defined functions. It is very easy to write the function in PHP .

Syntax of PHP function

PHP function starts with keyword "function" then the function name then parentheses or "round brackets" (parameter) , then the content of the function in braces or "curly brackets" { }.

function function_name(parameter) { statement; }

PHP function to display some characters

Write a PHP function that display a sentence "Hello world ! " .

<?php function myfirstfunction ?>{ echo "Hello world!"; } myfirstfunction;() // calling the function will display the ' Hello world! ' ?>; Output: Hello world!

Function that take two input numbers as as parameter and display their sum

We can pass the input values to the function as the parameters , these parameters pass in between the round brackets with coma ' , ' separated.

<?php function myaddfunction($a,$b) { echo $a+$b; } myaddfunction(5,4); // calling the function will display the sum ?>; Output : 9
php Functions Chapter Next »