Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP PHP Functions Introducing Functions PHP Function Default Arguments

Mark VonGyer
Mark VonGyer
21,215 Points

Why would I use default value?

Not sure why I would use the default value.

Would you call a function that needs data passed in if you aren't going to pass anything in?

Anyone used this in practice?

can you post the code for the function. more or less it depends what the function is doing. with out seeing the code for the function i cant really say.

Mark VonGyer
Mark VonGyer
21,215 Points

├Čt's generally speaking. I saw it on the video on PHP tutorial - default function value.

But why use a default? Is it to protect from errors in code?

1 Answer

Kevin Korte
Kevin Korte
28,135 Points

Sure, just about every CMS uses this. Take wordpress for example, there are probably literally hundreds of functions that are defined in Wordpress and available to use. Most of all of these functions have sensible defaults if arguments are not passed in.

Outside of WP, let's say you had a function that generated a menu. Most menus are generally the alike. This one function could generate two different menus depending on the arguments passed through.

A function is for reusable bits of code. Optional arguments just give the function the ability to be more customizable while still being DRY.