include関数を使う事でhtmlファイルをテンプレート化する事が出来ます。ページ数が多いサイトやコンテンツが増えていくようなwordpressなどのCMSでは必須の技術です。
include関数の使い方
外部ファイルを読み込ませたい箇所で、include関数を記述します。
<?php include("読み込ませたい外部ファイルのパス"); ?>
通常のHTMLファイル
<!doctype html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>include関数を使う</title> </head> <body> <header></header> <div class="main"></div> <footer></footer> </body> </html>
include関数を使ってパーツ化した場合
header.php
<!doctype html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>include関数を使う</title> </head> <body> <header></header>
footer.php
<footer></footer> </body> </html>
index.php
<?php include("header.php"); ?> <div class="main"></div> <?php include("footer.php"); ?>
コメント