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 trialGerard Dlima
11,908 PointsIs the final default else in a if-elseif mandatory in PHP or any coding language?
Is the final default else in a if-elseif mandatory in PHP or can I skip it like below withe the final else
if(condition) { //statement }
elseif(condition) { //statement }
2 Answers
Tony Nguyen
24,934 PointsYou can skip it and use an else if you'd like, however it really depends if there is another condition that you would like to test or not and this is speaking in general terms. If your challenge requires you to use an else if statement then you should use it just to pass.
Shawn Gregory
Courses Plus Student 40,672 PointsGerard
The final ELSE or an ELSE in a single IF statement will always be optional. It's there to allow you to have a block of code ran by default if none of your conditions are met. If you have an IF ... ELSE then the code block in your ELSE will run if the IF isn't met. Just-the-same, If you have 3 ELSEIFs (which by this point you should look into using a SWITCH statement), you can use a final ELSE to run some code if the initial IF and any of the ELSEIF's conditions weren't met. If you just want to test certain conditions and only want to run code if a certain condition is met, you can omit the final ELSE. Hope this helps.
Cheers!