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

undefined variable

I have just started working on my first PHP project and need a little help. I am trying to make a calendar but have been stuck on a certain aspect for awhile. Can someone please look at my code and explain why I am getting the errors that I am?

$day = date("d");
$month = date("m");
$year = date("y");
// Get the days of the month
$days_in_month = cal_days_in_month(CAL_GREGORIAN,$month,$year);

// Print each day
function print_days($days_in_month) {
    $x = 0;
        while ($days_in_month > $x){
            $x += 1;
            $date = '<div class="box">' . $x . '</div>';
        }
        return $date;
}

Error messages I am getting are as follows. Another thing to note is that I have echoed out all of the variables before the function and they are set correct.

Notice: Undefined variable: days_in_month in on line 13

function print_days($days_in_month) {

Notice: Undefined variable: days_in_month in on line 15

while ($days_in_month > $x){

Notice: Undefined variable: date in on line 19

return $date;

Any help would be appreciated. Not only would I love to get past this wall but understanding why would be a great thing to learn.

2 Answers

Here's what I did and it worked. Sorry it took so long, upgraded to OSX Yosemite recently and hadn't reconfigured this computer for development.

// Your variables were outside the function (outside of scope) so the function couldn't access them unless you passed them into the function.
function print_days() { // removed requiring the $days_in_month as we placed it inside the scope of the function
    $day = date("d");
    $month = date("m");
    $year = date("Y"); // 4 digit year using the upper case 'Y'
    // Get the days of the month
    $days_in_month = cal_days_in_month(CAL_GREGORIAN,$month,$year);

    $html = '';  // here's our html output, we will start empty and let the while loop populate it a piece at a time

    while ($days_in_month > $x){
        $html .= "<div class='box'>".$x."</div>\n"; // used '.=' so it appends this html to what was already in the $html variable, otherwise you will overwrite it every time you go through the loop. placed the '/n' at the end to make a new line in the html (easier to read), placed the html in double quotes to that the /n would work.
        $x++; // '++' means go up by 1
    }

    echo $html; // don't need to return anything if you want it to print the divs, just echo it here

}

print_days();

If you need any explanation please let me know.

I thought that including the variable inside the parenthesis of the function like I did made them available for the function, is that not the case?

Thanks so much for all of your help. Have taught me a bunch!

Looks like a problem with you defining $days_in_month check the syntax on the cal_days_in_month() http://www.php.net/manual/en/function.cal-days-in-month.php

if the syntax is correct, then maybe a value you're passing to it is off. I think it requires a 4 digit year and you might be passing a 2 digit year. date('y') = 2 digits, date('Y') = four digits. http://www.php.net/manual/en/function.date.php

I would do a for loop instead of the while. If you stick with the while then I would make the incrementing $x++; and place it as the last line of the while (convention).

Thanks for the help. But as I said, I echoed out all of the variables before the function and they were all correct (the $days_in_month was set to 31).

As for the loop, I will do some reading and try familiarize myself with the differences better and what the ++ does.