php includes for modularization cit 230 – web front-end development
TRANSCRIPT
PHP INCLUDESFOR MODULARIZATIONCIT 230 – WEB FRONT-END DEVELOPMENT
WHAT IS MODULARIZATION?
• Modularization is the idea of
separating a website into modules.
• A module can be a section that
repeats itself across a website.
• Example: Website headers and
website footers can be modules.
They are usually the same
throughout an entire website.
Website Body
Header
Footer
WHY USE MODULARIZATION?
• Imagine a 250-page website that needs an update in the footer.
• With html only, you’d have to edit every single page, which would be a
very tedious and error-prone task (even with copy/paste).
• It saves time, money, and is easy to maintain.
• It can be done with a PHP include.
WHAT IS A PHP INCLUDE?
• PHP is a scripting language used for web development.
• include is a command in the PHP language.
• It tells PHP to “include” a file.
• This is how a website can be modularized.
SYNTAX OF PHP INCLUDE STATEMENT
• The syntax is composed of 5 elements
1. The opening PHP tag: <?php
2. The include command itself: include
3. The file path and name in quotes: ’/modules/footer.php’
4. A semi-colon to end the PHP statement: ;
5. The closing PHP tag: ?>
• The result: <?php include ’/modules/footer.php’; ?>
WHERE TO INCLUDE THIS PHP INCLUDE?
• The include statement can go right in the html code.
• This is part of the file path. It tells the server to start at the root.
• Without this segment, the path would look something like this instead:
FILE STRUCTURE
• With PHP include, you only need
1 file per module.
• A suggestion is to keep them
organized into a module folder.
• Example: footer.php can be
designated to be the only footer
file across the entire website.
WHAT IS INSIDE THE MODULE FILE?
• The actual code that will be inserted!
• It is not an html file, so <!DOCTYPE>,
<html>, etc. are not needed.
• Think of it as a snippet of code that is
going to be inserted into a real html
file (the one with the php include).
EXAMPLE: BEFORE PHP INCLUDE
EXAMPLE: AFTER PHP INCLUDE
WHAT ABOUT THE PAGE SOURCE?
• Assuming the web host
supports PHP, visitors will
not see the php code in
the page source.
PHP leaves no evidence!
RESOURCES
• From PHP itself:
http://php.net/manual/en/function.include.php
• YouTube video on using PHP includes to modularize a website:
https://www.youtube.com/watch?v=R7k1K7UMs50
THANK YOU