LoginSignup
2
1

More than 1 year has passed since last update.

PHPでの「include」の使い方

Last updated at Posted at 2022-03-13

「include」の使い方

備忘録のためにメモしてます。

各ファイルで共通化している部分をbase.phpに記載します。

base.php
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title><?php echo $title ;?></title>
</head>
<body>
    <div class="container">
        <?php include $content; ?>
    </div>
</body>

タイトル部分に「$title」、bodyの中身を「$content」とし、呼び出し先の変数に置き換えることも可能。

index.phpファイルからbase.phpを読み込みたいときにincludeを使います。

index.php
<?php

$title = 'HogeeeWorld!!';
$content = __DIR__ . '/content.php';

include 'base.php';

$contentの中身は以下のファイルから読み込んでいます。

content.php
<h1>本文だよ!!</h1>

スクリーンショット 2022-03-14 0.39.27.png

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1