There are two types of functions available in the php language. The default library functions and user-defined functions. The library function is pre-defined function developed by PHP developer. You have to use the library functions as they have given to you by php. You can’t modify it. All library functions are available on the php.net website.
The user defines functions are opposite to the library function. You can simply put your own function name in it and run the code easily. The user defines functions allows you to customize the php code as per your need.
Let run the function code and understand its execution process:
1) Write the basic code in your php file.
<?php
?>
2) Now we will add the php function code.
<?php
function ()
{
echo "Hello World.";
}
?>
3) Next step is adding your own function name. The benefit of adding using user-defined function is you can add your own text or name to the function. In the below example I am using the text "type anything"
as my function name.
<?php
function typeanything()
{
echo "Hello World.";
}
?>
4) Now our function is ready to get executed. By looking at the above code you might think the code looks complete as we have already defined the echo in the function, so it should render the file by saying "Hello World"
. In php function, you have to use one more tag to present the result in the browser. The final code will look like this.
<?php
function typeanything()
{
echo "Hello World.";
}
typeanything()
?>
In the final code, I have added one more tag "typeanything()"
at the end of the function code which is generating the result. It is important to add a final tag to call the result.
When you execute the above code you will see the text that we have added in the echo statement is rendered in the browser.
This is what you get as a result when you run the function code.
Hello World.
If you see the result “Hello World”, it means your function code is executed successfully.
If you get an error while rending the code then check below code explanation step to find the reason for the error.
Code Explanation:
1) Default Function code:
<?php
function typeanything()
{
echo "Hello World.";
}
typeanything()
?>
The above code represents the basic code of the function. You have to use the code as it is without removing any of the element that mentioned in the code except the echo text “Your text here”. You can change the echo sentence by adding your own text.
2) Begining function tag:
function ()
– This is the tag that tells php file to run the code as a function. The php has stored the word "function"
as pre-define developer code so you can’t modify it as per your need. You have to use the code as it is. Avoid spelling mistakes while writing. The parenthesis "()"
are used to present block of the code that you will echo in the result. The word "function"
and parenthesis "()"
sign must use as per the given format. It is the default tag for the function.
3) Echo tag in function:
{
echo "Your text here";
}
To put the echo tag in the function simply use curly brackets "{}"
. The curly brackets used to present the type of code you are going to execute in the function.
4) Function output tag:
type anything()
The word "type anything"
can be changed as per your need. For instance, if you are running the function to present the username then simply use the word "username()"
to the function. The idea is making the function easy to readable for the human. The word will tell you what function does in the result.
The code is very important to display the function result. Echo statement will not produce the result in the absence of the final call tag. You have to mention the calling statement at least once to call the result of your function.
The user defines function comes in the three types.
1) No argument and no return: In this function, you simply put the basic function without adding any value to it. The function will run the code by showing the default text that has put in the code.
<?php
function typeanything()
{
echo "Hello World.";
}
typeanything()
?>
2) With argument and no return: In this type, you mention the argument to the user defined the function but you do not call the return tag to show the result.
<?php
function typeanything($company)
{
echo $company;
}
typeanything("mddir")
?>
3) With argument with return: The third type is typically used by the developer to produce the result. When it uses the function code will show you the argument value that you have put in the function code and the return value will produce the result of the argument.
<?php
function sum($x, $y) {
$c = $x + $y;
return $c;
}
$b = sum(4, 5);
echo $b;
?>
4) No argument with return: This function doesn’t require the argument value to generate the result.
<?php
function sum() {
$x = 6; $y = 7;
$c = $x + $y;
return $c;
}
$b = sum(4, 5);
echo $b;
?>