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

Alex Watts
Alex Watts
8,396 Points

PHP Function Arguments.

Hello, why would I need to pass an argument when calling the function? (see below):

function get_name($name) {
  echo "Hello, $name";
}
get_name("Alex");

Thanks!

Cody Te Awa
Cody Te Awa
8,820 Points

Hi Alex, Is there any way you could be more specific about your the question? From the looks of things, the reason an argument is necessary so that the echo statement can echo the supplied argument. Is there something else that is confusing you? :)

2 Answers

Mark Hill
Mark Hill
9,047 Points

Because in the function definition there is a required argument. You can make it optional by doing

function get_name($name = "default") {
  echo "Hello, $name";
}
Alex Watts
Alex Watts
8,396 Points

Thanks all for your help!