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
Welby Obeng
20,340 Pointshow come there is a null in array var_dump?
Take the look at the code below. If I comment out setMessage('test');and setMessage('test2'); var_dump returns false. Which makes sense. When I uncomment setMessage('test'); and setMessage('test2'); it returns :
test test2 NULL
Where did the null come from?
<?php
$error_messages = array();
function setMessage($message) {
global $error_messages;
$error_messages [] = $message;
}
function getMessage() {
global $error_messages;
if (empty($error_messages)) {
return false;
} else {
foreach ($error_messages as $message) {
echo '<div class="notify notify-red"><span class="symbol icon-error"></span>'.' '.$message.'</div>';
}
}
}
setMessage('test');
setMessage('test2');
var_dump(getMessage());
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Welby,
If your array is not empty then your getMessage function does not have an explicit return statement which means it will return the value NULL
Presumably you would want to return true at the end of this function to indicate that you had some messages as opposed to returning false when you don't have any.
Then your output could be:
test
test2
bool(true)
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsI looked at the question title again and realized I should clarify that you're not doing a var_dump of the array. You're doing a var_dump of the return value of that function.
This is either going to be
bool(false)orNULLdepending on whether you had messages or not.The output that you're seeing is the html output for "test" and "test2" from the
echostatement and then the var_dump of the return value from that function.