Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Optional Function Arguments

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

Optional Function Arguments

But right now, the contact page is broken because get_pets() has a required argument, and we’re not passing it anything. But what should we pass here? 1000? 1 million? We really want no limit, but that’s not possible right now.

So let’s be clever and pass 0:

<!-- contact.php -->
<!-- ... -->

<h1>
    ...your new best friend from over <?php echo count(get_pets(0)); ?> pets.
</h1>

Next, add an if statement in get_pets(). Let’s only add the limit if the $limit variable is set to something other than zero. We can do this by first creating a $query variable. Then, if we have a limit, add a little bit more to the new variable:

// lib/functions.php
function get_pets($limit)
{
    // ..

    // THIS IS A HUGE SECURITY FLAW - TODO - WE WILL FIX THIS!
    $query = 'SELECT * FROM pet';
    if ($limit != 0) {
        $query = $query .' LIMIT '.$limit;
    }
    $result = $pdo->query($query);
    // ...

}

Try it out! The contact page shows us the right number once again. Our function just got a bit more powerful.

Using Things that Look & Smell Like true/false in an If Statement

Whenever we have an if statement, we always pass it a boolean: either a variable or little bit of code that equates to true or false. When we use the != operator, PHP compares the values and feeds either true or false to the if statement. That’s old news for us.

But let’s try something. What if we just put the $limit variable in the if statement:

// lib/functions.php
// ...

if ($limit) {
    $query = $query .' LIMIT '.$limit;
}

So $limit isn’t a boolean: it’s either the number 3 or 0, depending on who’s calling it. So will this work? Will we get an error?

Refresh the contact page. No error - and it shows 4 pets. Now go to the homepage. We see 3 pets. Things are still working!

So you can pass something other than true or false to an if statement. And when you do, PHP looks at what’s there and tries to figure out if it looks and smells more like true or more like false.

For example, empty things like an empty string or the number zero becomes false. Everything else becomes true.

Value Equates to
0 false
empty string false
empty array false
null false
false false
5 true
foo true
array(5, 10) true
-25 true
true true

Even a negative number or a string that has only spaces in it becomes true, because these aren’t really empty. So when we pass 0 for the $limit argument, it looks like false and doesn’t enter the if statement.

Optional Argument

But we can go further and make the $limit argument completely optional. How? Just assign it a value when you declare the argument in get_pets():

function get_pets($limit = 0)
{
    // ...
}

This doesn’t automatically change anything. But now we can remove the argument we’re passing to get_pets() in contact.php:

<!-- contact.php -->
<!-- ... -->

<h1>
    ...your new best friend from over <?php echo count(get_pets()); ?> pets.
</h1>

Prove that it works by going to the contact page.

The Nothing Value: null

Let’s change the 0 default value to null. null is the value that is equal to “nothing”, and I used it earlier as my database password, since my MySQL server doesn’t have a password.

Both 0 and null will look like false in the if statement, so the code will act the same. Since null means nothing, it just feels a little bit better to use it as the default value for an optional argument.

The Function Signature

By the way, the name and arguments to a function are called the “signature”. When you hear people talking about a function’s signature, it’s just a smart-sounding way to refer to the arguments that function has and also the value it returns. So now you’ll sound even smarter when talking with your cool new programmer friends.

Leave a comment!

6
Login or Register to join the conversation
Moses O. Avatar
Moses O. Avatar Moses O. | posted 2 years ago

Just realised that the sql query you write that is held by the $query variable inside the if statement needs to be spaced or else you will get errors.
Its a small realization but it took me a while to correct since my code and the tutors code were identical and just the space was the issue.
$query=$query.' LIMIT '.$limit; //(WHEN WRITING LIMIT--LEAVE A SPACE AFTER AND BEFORE THE INVERTED COMMAS..

Reply

Hey Not.available_X,

Yeah, you're absolutely right! We do have those spaces around the LIMIT word, but it's easy to miss :) Thanks for focusing attention on this tricky part of the code.

Cheers!

Reply
Haris R. Avatar
Haris R. Avatar Haris R. | posted 3 years ago

When you assign the argument with a value of null. How would the if statement ever become true in the scope of the function?
Would not the parameter's value be overwritten by the argument always?

1. function test(param = null){ //Wouldn't the null value will overwrite the paramater's value which is 3 (line 7)?
2. if (param==true)
3. { //How would this ever become true? }
4. }
5.
6. //function call
7. test(3);

Reply

Hey Haris R.

Whenever you define a function argument as follows:


function test($param = null)
{
...
}

what it does is to define the default value of the $param argument. So, it allows you to call the test function as follows


test(); // it will use the default value null 

test('hello'); // no default value will be assigned because you passed a value

I hope it makes sense to you :)
Cheers!

Reply
Haris R. Avatar

Yepp.. Got it! Should've been explained in the tutorial as it is for the beginners

Reply

Oh, you are right, sorry for the confusion. At least I could answer your doubts :)

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

userVoice