PHP Function Scope

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:05
    with Leanna Pelham
  • 0:09
    OK.
  • 0:09
    Now to create that get_pet function.
  • 0:14
    Add it in functions.php.
  • 0:20
    First, let's build the query.
  • 0:23
    Instead of returning every row in a table,
  • 0:25
    we can use the WHERE clause trick we learned earlier
  • 0:28
    to return only the row whose ID equals the ID variable argument.
  • 0:33
    Like before, this query has a variable part to it.
  • 0:37
    So it is a security flaw, but we're going to fix that in a few seconds.
Loading ...

PHP Function Scope

Loading...

Our last big piece to the puzzle is to create a function to query the data for just one pet. In the process of doing this, we're going to get a lesson in function scope, where each function is its own little universe and you only have access to the arguments passed in and any variables you create in that function. This can trip you up when you need to access that data from another function. In this tutorial, we'll get that all sorted out and finish up our get_pet() function.

Downloads: 
Log in or sign up to download companion files.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.