Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Anthony Meyer
Anthony Meyer
2,472 Points

Getting errors on outputs that aren't supposed to return anything

Hello all! Quick question regarding errors that I am noticing. In some of the lessons, I am receiving errors on previews when I shouldn't be getting anything at all, or I should be getting something like "0". I'll post the code I'm using as well as an example of a lesson that returns these errors.

https://teamtreehouse.com/library/php-functions-2/function-returns-and-more/php-variable-functions

<?php

function answer(){ return 42; }

function add_up($a, $b){ return $a + $b; }

$func = 'add_up';

$num = $func();

echo $num;

This code gives me the following result at 4:37 in the link that I provided.:

Warning: Missing argument 1 for add_up(), called in /home/treehouse/workspace/index.php on line 13 and defined in /home/treehouse/workspace/index.php on line 7

Warning: Missing argument 2 for add_up(), called in /home/treehouse/workspace/index.php on line 13 and defined in /home/treehouse/workspace/index.php on line 7

Notice: Undefined variable: a in /home/treehouse/workspace/index.php on line 8

Notice: Undefined variable: b in /home/treehouse/workspace/index.php on line 8 0

However, the video shows the output as "0". Why does this happen? Is it a result of the actual language having been updated? I noticed that the video itself is a year or two old, so is that part of the problem? Is there anything that I'm doing wrong? Thanks in advance everyone!

1 Answer

Fletcher Henneman
Fletcher Henneman
3,130 Points

You specified 2 parameters for the add_up function. Those are a and b. (function add_up($a, $b))

If a function has a parameter, you must pass it a variable when calling it. So when running add_up() you must pass it two numbers.

For example) add_up(5, 13) which will return 18.

Now in the video where it outputs "0", I'm not sure 100% why that is happening. It could be that since the variables were not specified, they default to 0. Also, it could return 0 as some type of error. Here is a list of possible reasons his function is returning 0: PHP Empty Docs

Anthony Meyer
Anthony Meyer
2,472 Points

Thank you! I was really just asking for my own benefit. I'm pretty diligent about checking my code against the code in the videos, but sometimes I still get some odd results that are different from the material, so I didn't know what was causing it. Sorry for the late response!