You are on page 1of 1

PHP Functions - Parameters

Another useful thing about functions is that you can send them information that the function can then use.
Our first function myCompanyMotto isn't all that useful because all it does, and ever will do, is print out a single,
unchanging string.
However, if we were to use parameters, then we would be able to add some extra functionality! A
parameter appears with the parentheses "( )" and looks just like a normal PHP variable. Let's create a new
function that creates a custom greeting based off of a person's name.
Our parameter will be the person's name and our function will concatenate this name onto a greeting
string. Here's what the code would look like.

PHP Code with Function:


<?php
function myGreeting($firstName){
echo "Hello there ". $firstName . "!<br />";
}
?>

When we use our myGreeting function we have to send it a string containing someone's name, otherwise
it will break. When you add parameters, you also add more responsibility to you, the programmer! Let's call our
new function a few times with some common first names.

PHP Code:
<?php
function myGreeting($firstName){
echo "Hello there ". $firstName . "!<br />";
}
myGreeting("Jack");
myGreeting("Ahmed");
myGreeting("Julie");
myGreeting("Charles");
?>

Display:
Hello there Jack!
Hello there Ahmed!
Hello there Julie!
Hello there Charles!

It is also possible to have multiple parameters in a function. To separate multiple parameters PHP uses a
comma ",". Let's modify our function to also include last names.

You might also like