Ah, my editor is highlighting the $pets variable because it’s undefined.
A function only has access to the variables you create inside of it, and,
yep, there’s definitely no $pets variable here. It lives back in pets_new.php.
We already know that functions can have arguments - we just saw that with
header, which has one argument. We can make save_pets require an
argument too - just add a $petsToSave variable between the parenthesis
of that function:
Things work the same as before, so why did I make you add this function?
Moving logic into functions gives us 2 really cool things.
First, if we need to save pets somewhere else, we can just re-use this function.
We’re already doing this with get_pets, which I think we’re calling in
at least 3 places.
Second, by moving these lines into a function with a name, it helps explain
what they do. If I didn’t write this code, I might have trouble figuring
out what it does or its purpose. But when it’s inside a function called
save_pets, that helps.
We’ll go through this process of writing code and reorganizaing it over and
over again. Our site is measured by more than just whether or not it works:
you also want your code to be easy to understand and easy to make more changes
Building great things and doing it well, that’s where you’re going. And
you’ve just finished another episode you crazy developer! Congrats! Now
build something and keep learning with us.