All TalkersCode Topics

Follow TalkersCode On Social Media

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

User Defined Function In PHP

Last Updated : Mar 11, 2024

User Defined Function In PHP

In this article we will show you the solution of user defined function in PHP, now we have to declare function call which is get loaded when user executes program on browser and each function had two different type of parameters then in function definition we printing passed arguments on webpage using echo statement.

Just follow the step by steps guide below.

Step By Step Guide On User Defined Function In PHP :-

Here we declared two function calls ‘fun(),fun1()’ with two different types of parameters like integers, strings.

In fun() function definition we specified integers ’24,12’ as parameters then within function we adding both integers by referred variables ‘$a1, $a2’.

In fun1() we specified string arguments then in definition we concatenates both by ‘.(dot)’ symbol then we printed using echo.

<?php
function fun($a1,$a2){
    echo $a1+$a2 . "<br>";
}
function fun1($a1,$a2){
    echo $a1." ".$a2;
}
fun(24,12);
fun1("Hi","Everyone");
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we defined function calls ‘fun(24,12), fun1("Hi","Everyone")’, we can see both had different types of parameters.
  4. We defined fun($a1,$a2) method, here variables ‘$a1 refers value 24, $a2 refers value 12’ and within method we adding both of them at echo statement so it prints sum of ’24,12’ on webpage.
  5. We defined fun1("Hi","Everyone") method, here variables ‘$a1 refers string Hi, $a2 refers string value Everyone’ and within method we concatenates both of them at echo statement by (.(dot)) symbol so it prints both strings with space on webpage.
  6. If you doing concatenation or just printing passed parameters or without parameter within method printing anything like string or numbers on function definition then you can use one common function we gets result without error.
  7. Else we doing some calculation with different types of parameter means then it throws error because it will not suitable for all type arguments.
  8. We can also define common user defined function then within that we just print both types of values on webpage but here we need to concentrate on another thing is if we defined common method there we doing sum for two different type parameters it throws error webpage so define method commonly work for all type parameters.

Conclusion :-

In conclusion now we are able to know how to define user defined function and how to use in php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser it will prints result of ’36, Hi Everyone’.

We can also modify above user defined like we seen in last point or just modify above defined function with some other values as per your choice we will definitely gets result accordingly without error in this concept.

I hope this article on user defined function in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪