Introduction To PHP Functions
Introduction To PHP Functions
Defining Functions
Let’s get right to it and create our first function:
function greetLearner()
{
echo "Hello, Learner!\n";
echo "I hope you're enjoying PHP!\n";
echo "Love, Codecademy";
}
Let’s walk through the code above:
In the next exercise, we’ll show you how to invoke a function. For now, let’s
get some practice defining them!
Instructions
1.
Define a function named praisePHP. You can leave the function body blank
for now.
Stuck? Get a hint
2.
Add at least one echo statement inside your function body which prints a
string praising the PHP language.
Stuck? Get a hint
3.
Now that your function is fully defined, run your program. Notice what
happens… or really what doesn’t happen.
INTRODUCTION TO PHP FUNCTIONS
Return Statements
As we build more complicated functions, we’ll often be using them to
process data. In order for the data to be useful, functions have the ability
to return a value in addition to performing instructions. Let’s look at an
example:
function countdown()
{
echo "4, 3, 2, 1, ";
return "blastoff!";
}
When the countdown() function is invoked it will print 4, 3, 2, 1,, but what
about the string "blastoff!"? This value will be returned. We have a lot of
options for what to do with a returned value. For example, we could
capture it in a variable:
$return_value = countdown(); // Prints: 4, 3, 2, 1,
echo $return_value; // Prints: blastoff!
This example is a little silly, since we could have just printed the string
within the function, but, as we continue to create more complicated
functions, the ability to return a value will become extremely useful.
Instructions
1.
Write a function printStringReturnNumber() which prints a string and returns a
number value.
Stuck? Get a hint
2.
Capture your function’s return value in a variable named $my_num.
Stuck? Get a hint
3.
Use echo to print your $my_num variable.
More on Return Statements
The return keyword immediately stops a function. This means that any code
after a return won’t run.
$first_result = announceRunning();
echo $first_result;
Let’s walk through the code above:
$second_result = announceRunning2();
echo $second_result;
In this example, the string "P.S., I love you" will never be printed! As soon
as the return statement is reached, the function will end. So in the terminal,
we’d see this output:
This is the return value of the second function.
Let’s use this new knowledge to fix some broken code!
Instructions
1.
Run the code to see its current output.
Stuck? Get a hint
2.
The notFound() function should print the string "ERROR: Page not found!\n" and
return 404. Fix the notFound() function so that it works as intended.
Stuck? Get a hint
3.
Great work! The greetLearner() function also isn’t quite right… We’d like all
three of those echo statements to execute and the function to return "<3".
Help!
Return Values
The value returned from a function is just that—a value. This means it can
be used in any manner we would normally use a value of that type. This can
take some getting used to. Take a look at the following code:
function returnFive()
{
return 5;
}
$num = 5;
Instructions
1.
Use echo to print the return values of each of the three provided functions in
order. The challenge: you may not use more than one line of code or more
than a single statement (use only one semicolon).
Note: You can add space, new line, or other characters in between the
return values.
Returning NULL
What about functions without return statements? Any function without
a return returns a special value NULL. NULL is a special data type that stands for
the absence of a value.
function returnNothing()
{
echo "I'm running! I'm running!\n";
}
Instructions
1.
Write a function createVacuum() which returns nothing.
2. NULL can be really quirky. You don’t need to worry about the details now.
But, just for fun, uncomment the line we provided at the bottom of the
code. Predict what it will output to the terminal. When you have a guess,
run the code to see.
Parameters
Functions that do exactly the same thing every time they run can save us
from having to repeat code in our programs, but functions can do more.
1.
Write a function increaseEnthusiasm() which takes in a string parameter and
returns that string appended with an exclamation mark.
Stuck? Get a hint
2.
Use echo to print the result of invoking your increaseEnthusiasm() function
with a string of your choice.
Stuck? Get a hint
3.
Write a function repeatThreeTimes() which takes in a string parameter and
returns that string repeated three times (without introducing characters
which didn’t appear in the original string).
Stuck? Get a hint
4.
Use echo to print the result of invoking your repeatThreeTimes() function with a
string of your choice.
Stuck? Get a hint
5.
Ready for a little trickiness? Use echo to print the result of invoking
your increaseEnthusiasm() with the result of invoking repeatThreeTimes() as the
argument passed into increaseEnthusiasm(). You can choose any string you
like for the argument to repeatThreeTimes().
Pass By Reference
We can invoke functions with variables or with values directly. When we invoke a function
with a variable as its argument, it’s as if we’re assigning the value held by that variable to the
function’s parameter. We assign a copy of the value held by the argument variable. The
variable argument and the parameter are distinct entities; changes made inside the function to
the parameter will not affect the variable that was passed in:
If we do want to make permanent changes to a variable within a function, we can prepend the
parameter name with the reference sign (&). In this way, we assign the parameter to be an
alias for the argument variable. Both will refer to the same spot in memory, and changes to
the parameter within the function will permanently affect the argument variable.
convertToQuestion($example);
function convertToQuestion(&$str)
convertToQuestion($string_one);
convertToQuestion($string_two);
convertToQuestion($string_three);
echo $string_one;
echo $string_two;
echo $string_three;