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

PHP PHP Functions Introducing Functions PHP Function Default Arguments

Mark VonGyer
Mark VonGyer
21,239 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,239 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,148 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.