All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Function

PHP has two types of functions PHP Built-In functions and PHP user defined functions.


In this chapter we will discuss PHP user defined functions.You can make your own functions and use them whenever and wherever you want just simple by making a function and calling that function.


Syntax of Creating a Function

function functionName() 
{
    Your code here;
}

/*For calling your function*/
functionName();

Example of PHP Function

function demo()
{
  echo "Hello PHP";
}
demo();

This will display Hello PHP.




Syntax of Function with parameters

function functionName($val) 
{
    echo $val1;
}

/*For calling your function*/
functionName(val);

You can send as many parameters in function as you want.


Example of PHP Function

function demo($val1)
{
  echo "Here is your value ".$val1;
}

demo(10);

This will display Here is your value 10.




Example of PHP Function Return Value

function demo($val1)
{
  $add=$val1+10;
  return $add;
}

$val2 = demo(10);
echo "Here is your value ".$val2;

This will display Here is your value 20.




Setting Default Value to PHP Function

function demo($val1=10)
{
    echo "Here is your value ".$val1;
}
 
demo();
demo(5);

This will display 10 in first call and 5 on second call.

❮ PrevNext ❯