Log in from a computer to take this course

You'll need to log in from a computer to start Learn PHP. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store
Learn

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.

Let’s get some practice returning a value from a function!

Instructions

1.

Write a function printStringReturnNumber() which prints a string and returns a number value.

2.

Capture your function’s return value in a variable named $my_num.

3.

Use echo to print your $my_num variable.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?