FAQ: Introduction to PHP Functions - Returning NULL

This community-built FAQ covers the “Returning NULL” exercise from the lesson “Introduction to PHP Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn PHP

FAQs on the exercise Returning NULL

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

what i didn’t understand that how the statment ($result = returnNothing(); ) Prints: I’m running! I’m running!
i mean… where is the echo statment we just define ($result) and assign the value to it but there is no echo or somthing else.

Hello, @majendy, and welcome to the forum. I believe this example code is what you’re referring to:

function returnNothing() 
  echo "I'm running! I'm running!\n";

$result = returnNothing(); // Prints: I'm running! I'm running!

echo $result; // Nothing is printed

The function returnNothing() has an echo statement in it’s code body. Any time returnNothing() is called, the echo "I'm running! I'm running!\n"; is executed. The $result variable is assigned the return value of the function. The function has no return value, so $result = NULL. We could add a return statement to the function if we wanted to, and that value would be assigned to $result, and then the last line of code: echo $result would print that value.
Like this for example:

function returnNothing() 
  echo "I'm running! I'm running!\n"; //prints "I'm running! I'm running!" when the function is called
  return "Okay. I'm finished running.\n"; //returns "Okay. I'm finished running.\n" to the line of code that called the function.

$result = returnNothing(); // calls the function, the return value is assigned to $result

echo $result; //  prints the value assigned to $result


I’m running! I’m running!
Okay. I’m finished running.

Hope this helps!

1 Like

ohh yes that was helpful thx for your answer

1 Like

Hi, I am not getting this, I am doing everything as per example:


// Write your code below:
function createVacuum() {
  echo "I have created a vacuuum";
$result = createVacuum();
echo $result;
echo createVacuum() * 10;


I have created a vacuuumI have created a vacuuum0

The second should not print 0 and the third 10 zeros?

Hello, @tuzso.

Your code includes 2 calls to your createVacuum() function. Inside the function you echo the string, "I have created a vacuuum". You call the function the first time here:

 $result = createVacuum();

Since the function is called, “I have created a vacuuum” is printed. That accounts for the first part of your output.
Next, you echo $result. What value was assigned to $result? Answer: NULL Since createVacuum() has no return value, NULL is returned. When we echo NULL nothing is printed, so you still have just “I have created a vacuuum” as your output.
Then, you have this line:

echo createVacuum() * 10;

createVacuum() is again called, so its echo statement is executed, and “I have created a vacuuum” is once again printed. Now your output looks like this:

I have created a vacuuumI have created a vacuuum

After the function is finished executing it once again returns NULL, so your line of code is essentially now this:

echo NULL * 10; //since NULL is the return value of createVacuum()

That expression evaluates to 0, so 0 is printed which gives you your final output:

I have created a vacuuumI have created a vacuuum0

I took the liberty of editing your post so that the code would display properly. For future posts please see How do I format code in my posts?