PHP tutorials
Question:
How do you create and call a custom defined function in PHP?
Answer:
A simple function
This simple function takes no arguments and returns no result. The (commented out) stars either side of the function declaration are there to make the function declaration stand out.
<?php
//***********************************
function myFunction1()
//***********************************
{
echo '<p>This is the body of myFunction!</p>';
}
?>
Calling the function
Calling the function is very straightforward, and is done like this
<?php myFunction1(); ?>
Output
This is the body of myFunction!
A function that takes an argument
An argument is what you pass a function to act upon. myFunction2 displays the value of the argument on the browser.
<?php
//***********************************
function myFunction2($parameter)
//***********************************
{
echo '<p>This is the body of myFunction2 with the parameter, ' . $parameter . ', passed to it!</p>';
}
?>
Calling the function - technique 1
Calling myFunction2 can be done in a couple of ways providing that an argument is passed to the function.
<?php
myFunction2('someValue');
?>
Calling the function - technique 2
<?php $var1 = 'someValue'; myFunction2($var1); ?>
Output
This is the body of myFunction2 with the parameter, someValue, passed to it!
Default Arguments
Default arguments allow you to call the function while making the parameter(s) you pass to it optional. That is, you can call the function with or without the argument, and if you don't supply an argument, then the function will use the default one you specified when you first created the function.
<?php
//***********************************
function myFunction3($parameter = '')
//***********************************
{
if($parameter == '')
echo '<p>You didn\'t pass anything to myFunction3!</p>';
else
echo '<p>You passed ' . $parameter . ' to myFunction3!</p>';
}
?>
Calling the function - technique 1
In this example we call the function without passing any arguments.
<?php myFunction3(); ?>
Output- technique 1
You didn't pass anything to myFunction3!
Calling the function - technique 2
In this example we call the function and pass an argument to it.
<?php
myFunction3('someVar');
?>
Output- technique 2
You passed someVar to myFunction3!
Returning a Value
Functions can also return a value, and this is done with the return keyword. In the example below, the function returns 'LARGE' if the length of the parameter passed to the function is greater than 10 characters, otherwise it returns 'SMALL'
<?php
//***********************************
function myFunction4($parameter)
//***********************************
{
if(strlen($parameter) > 10)
return 'LARGE';
else
return 'SMALL';
}
?>
Calling the function - technique 1
<?php $var1 = 'someVar'; if(myFunction4($var1) == 'LARGE') echo "<p>$var1 is a large word</p>"; else echo "<p>$var1 is a small word</p>"; ?>
Calling the function - technique 2
<?php $var1 = 'someVar'; $result = myFunction4($var1); echo "<p>$var1 is a $result word</p>"; ?>
Calling the function - technique 3
<?php echo "<p>$var1 is a " . myFunction4($var1) . " word</p>"; ?>
Output
someVar is a small word
Additional Notes
If you pass too many or too few arguments to a function, then you'll receive a warning from PHP.
Comments or questions relating to this article have been disabled. They will be back soon.
Copyright
Let notice be given that this is copyright information