PHP Include & Require

What are Include and Require?

include and require allow you to reuse code by inserting one file into another.

This helps organize your code and avoid repetition.

Basic Include

Loading...
Output:

Basic Require

Loading...
Output:

Include vs Require

  • include → warning if file missing, script continues
  • require → fatal error if file missing, script stops
Loading...
Output:

include_once and require_once

These ensure the file is included only once.

Loading...
Output:

Real-World Example

Loading...
Output:

Including PHP Logic

Loading...
Output:

Full Example

Loading...
Output:

Why Include & Require are Important

  • Reuse code
  • Keep projects organized
  • Separate layout and logic
  • Used in real applications

Common Mistakes

  • Using include instead of require for critical files
  • Including files multiple times
  • Incorrect file paths

Practice

Create header and footer files and include them in a main page.

Loading...
Output:

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic