1. 37
    Including or requiring files in PHP
    4m 18s

Including or requiring files in PHP

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Files can get hard to read & debug if they get too long. In this lesson, we'll learn how to include external files in your PHP scripts.

Instructor: [0:00] Up until now, all of our code has been written in a single PHP file. Obviously as our app grows, this won't work and you'll probably need to organize your code into multiple files.

[0:13] Specifically, these functions really take up a lot of space. We can collapse them, but it's much better if you organize them into their own file.

[0:22] Within our project, let's go ahead and create a new PHP file named functions.php. This file will be created in the root of our app, right next to the index.php file.

[0:39] Let's go to our index.php and copy the top contents of this file over to functions.php.

[0:49] I like to space out my functions with line breaks and properly indent all of my code with four spaces. This makes code easier to see and reason about when you are looking through it.

[1:02] White space is removed by the PHP processor, so you can add as many line breaks and indents as you wish.

[1:10] Let's go ahead and save this file, and then we will return back to our index.php file. We can go ahead and remove the code that contains our functions.

[1:22] We will leave the declare strict_types line at the top of this file, because I think keeping that line at the top of every PHP file in our app leads to more reliable applications.

[1:34] Now if we save and refresh our page, we can see that we get an error that says call to undefined function getPosts on line four.

[1:45] We actually have multiple errors occurring in this file, but PHP only outputs the first error that occurs, and then halts the remaining execution of the script.

[1:56] You probably figured out that this getPost function is not found, because there is no reference to the functions.php file that we created that contains the getPost function, along with the getPost text function.

[2:11] There are two ways to include a file in PHP and they're both language constructs. One construct is named include, and the other is named require.

[2:22] You can call these constructs just like regular built-in PHP functions. The difference between the two is that require requires the file to exist, and include doesn't.

[2:35] For example, at the top of our file if we write include and then pass in my file.php. If we save and refresh the page, notice that PHP outputs a warning.

[2:54] It still continues the execution of the script as our call to undefined function getPost error still occurs. If we change this include line to require and save and refresh the page, that call to undefined function error never occurs because further execution of the program is stopped.

[3:17] This means that include allows you to include optional files that may or may not exist, but require has a hard requirement on that file existing. There are use cases for each, but when in doubt, always use require as it will make your code more consistent and reliable in the long run.

[3:37] Let's go ahead and change my fancy file to functions.php. This is the new file that we created that contains our two functions, getPosts and getPostText. When we actually call these functions in our code, it will find them and execute it properly.

[3:59] When we reload, we will see we are back to our properly working app. The results of writing this code is essentially the same effect as having functions written out directly within our index.php file, but allows us to better organize and structure our code.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today