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 trialROBERT RUBY II
10,586 PointsWhats wrong here?
Confused as to why this is giving me an optional parameter error?
function get_people( $teachers = 0 ) { include 'connection.php';
//add code here
if( ! $teachers ){ $q = $db->prepare('SELECT * FROM people ORDER BY last_name DESC'); }else{ $q = $db->prepare('SELECT * FROM people WHERE treehouse = 1 ORDER BY last_name DESC'); } $q->execute(); return $q->fetchAll(PDO::FETCH_ASSOC); }
<?php
function get_people($teachers = 0) {
include 'connection.php';
//add code here
if( ! $teachers ){
$q = $db->prepare('SELECT * FROM people ORDER BY last_name DESC');
}else{
$q = $db->prepare('SELECT * FROM people WHERE treehouse = 1 ORDER BY last_name DESC');
}
$q->execute();
return $q->fetchAll(PDO::FETCH_ASSOC);
}
2 Answers
jonathanbarrios
Treehouse Teacher👋 ROBERT RUBY II,
You are very close! If you set the $teachers variable to NULL, it will only return the teachers if $teachers = 1. I tested it using NULL and it passed.
<?php
function get_people($teachers = NULL) {
include 'connection.php';
//add code here
if( ! $teachers ){
$q = $db->prepare('SELECT * FROM people ORDER BY last_name DESC');
}else{
$q = $db->prepare('SELECT * FROM people WHERE treehouse = 1 ORDER BY last_name DESC');
}
$q->execute();
return $q->fetchAll(PDO::FETCH_ASSOC);
}
I hope this helps and happy coding! 🙌
jonathanbarrios
Treehouse Teacher👋 ROBERT RUBY II,
Way to go! Polyglots rule 🎉
Instructions: Add an OPTIONAL parameter to accept if the function should return only people who have been Treehouse teachers.
Here's some more information about comparing NULL to 0(FALSE). If you use NULL the behavior is OPTIONAL and if you use 0 if will always be FALSE and not OPTIONAL.
NULL means "nothing" or the var has not been initialized.
FALSE means "not true" which is boolean, for logical issues.
Try this
<?php
if(0===NULL) {
echo "=== True";
} else {
echo "=== False";
}
echo "<br>";
if(0==NULL) {
echo "== True";
} else {
echo "== False";
}
I hope this was useful! Happy coding! 🙌
ROBERT RUBY II
10,586 PointsROBERT RUBY II
10,586 PointsYep I figured this out by looking through the videos again. I've never done this, always used an empty String or 0.
Is there a drawback to doing it this way?
I have been working with PHP and Wordpress for years on and off while in a separate profession, I am not looking to travel anymore so I'm trying to change the paradigm of my work life by landing a remote developer position.
Glad to see something structured as I have been filling in wholes in my knowledge... Like PDO and SQL in general for example... Working with WP_QUERY I never had to worry about these things.
I touched on them during my PHP learning phase before I found Wordpress.
Enjoying the courses and looking forward to being recognized as a polyglot in the near future. I have intentions of working through each tech degree over the next year.