Using require to Include Functions

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:02
    to Include Functions with Leanna Pelham.
  • 0:07
    LEANNA PELHAM: I'll show you a few more
  • 0:09
    tricks with Functions in a second.
  • 0:12
    But right now, let's improve our contact.php
  • 0:14
    page we started earlier.
  • 0:21
    First, I want to be able to have a header that reads "Helping you find
  • 0:24
    your new best friend from over 500 pets."
  • 0:31
    Except I want the number 500 to be dynamic, reflecting the true number
  • 0:35
    of pets we have in our pets.json file.
  • 0:38
    I'll do this by calling our get_pets function
Loading ...

Using Require to Include Functions

Loading...

We want to update our contact page with the number of pets we currently have, which means we'll need to dynamically count our pets. We can use the get_pets() function to get us that information, but we're going to have to learn how to load the function from one file for use on another file. We're going to create a new home for our functions, in a functions.php file, to keep them centralized, and then we will use the require statement to use that file when we need it. We'll also discuss the other statement you can use in this instance—require_once, include, and include_once—and why we're choosing require over the others.

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

Tip: Why is not closing your PHP tags better when you don’t have to? Great question — see StackOverflow: Why would one omit the close tag?