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
Emily Williams
Courses Plus Student 3,968 PointsBuild a Simple PHP Application > Wrapping up the Project > Objects Code Challenge
I was just wondering about this code Both codes do the same thing and I get the same result for both of them. I was just wondering what way is more professional thanks.
<?php
$num = 17;
require_once('class.palprimechecker.php');
$checker = new PalprimeChecker();
$checker -> number = $num;
echo "The number $num ";
if(!$checker -> isPalprime()) {
echo "is not Palprime";
} else {
echo "is Palprime";
}
?>
or to have it like this
<?php
require_once('class.palprimechecker.php');
$checker = new PalprimeChecker();
$checker -> number = 17;
echo "The number " . $checker->number . " ";
if(!$checker -> isPalprime()) {
echo "is not Palprime";
} else {
echo "is Palprime";
}
?>
Both codes work and I get the same result just wondering what is more professional Thanks
5 Answers
Randy Hoyt
Treehouse Guest TeacherThanks, @Emily! Glad to hear it. :~)
Alexander Smith
2,769 PointsThe first one. You want to limit the number of hard coded things. It's much easier to change a variable at the top than it is to dig through code to find the value.
Emily Williams
Courses Plus Student 3,968 PointsAwesome thanks so much!!
Randy Hoyt
Treehouse Guest TeacherHey @Emily,
I don't like this line from the first example:
echo "The number $num ";
The $checker->isPalprime() method is going to check the value in $checker->number, so it makes much more sense to display the value from there instead of the value in the $num variable. (They have the same value right now, so it works: but it's possible that a line of code could added in the future to make them diverge.) Change that to this:
echo "The number " . $checker->number . " ";
The other issue is whether you need a variable called $num to hold the value 17. It depends on what makes the code easier to maintain. If someone is going to be changing the value in the code periodically, it might be night to define the value at the top of the file like you have done in the first example. With this code block, though, I don't think that's an issue: I'd recommend your second example.
Emily Williams
Courses Plus Student 3,968 PointsAwesome thanks so much love your tutorials :)