You are currently viewing How to Create PHP Function With Parameters | Examples
  • Post category:PHP

How to Create PHP Function With Parameters | Examples

Create PHP Function With Parameters

If you don’t know how to create a php function, this article is for you. This article is about create a php function with parameters.

When we need to do the same task multiple times, we can create a function. Whenever we need to do that work, we just call that function and that work will be done again.

How to create a PHP function

Syntax of Creating function

function functionname($para1, $para2, ...)
{
 Block of statement;
}

Syntax of calling function

functionname(argument1, argument2, ...);

Example

<?php
    function display()     // creating without parameter function
    {
          echo "welcome to Wonderdevelop";    
    }
    display();   // calling without parameter function

    function display($name1, $name2)     // creating parameter function
    {
          echo "$name1 to $name2";    
    }
 
   display();   // calling without parameter function
   display("welcome", "wonderdevelop" );   // calling parameter function

/*
Output:
welcome to Wonderdevelop
welcome to Wonderdevelop
*/
?>

Steps:

  1. Here, we have created two PHP functions, one is parameterized function and the other one is without passing a parameter function.
  2. Inside the without parameterized function, we printed some text. Inside the parameterized function, we printed two passed parameters.
  3. Then we call the function using the above syntax. First, we called without parameter function then we called with parameterized function with passing two arguments namely “welcome” and “wonderdevelop”. It will be executed like “welcome to Wonderdevelop”.

Leave a Reply