では4つのファイルを作りますよ
header.php
main.php
footer.php
functions.php
mainから
<?php
require_once("functions.php");
get_header("title");
?>
titleを渡して表示させたい
どうする?
→
できた。
--->main.php自分版
<?php
require_once("functions.php");
$foo=get_header("title");
?>
<body>
<h1>ファイルの読み込み</h1>
<title><?php echo $foo; ?></title>
</body>
<?php
get_footer();
?>
--->functions.php自分版
<?php
function get_header($hoge){
require_once("header.php");
return $hoge;
}
?>
<?php
function get_footer(){
require_once("footer.php");
}
?>
--->header.php自分版
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
</head>
--->footer.php自分版
</html>
でもこれだとbodyのなかにtitleが記述されてしまうな。表示はされてるけど
北側列:
ssid:Buffalo-A(G)-b34E
key:7s8u85nhderbt
7s8u85nhderbt
--->main.php自分版2
<?php
require_once("functions.php");
$foo=get_header("title");
?>
<title><?php echo $foo; ?></title>
</head>
<body>
<h1>ファイルの読み込み</h1>
</body>
<?php
get_footer();
?>
しかしこれは反則。さて。
--->main.php(正解)
<?php
require_once("functions.php");
get_header("title");
?>
<h1>ファイルの読み込み</h1>
<?php
get_footer();
?>
require_onceとは:
ファイルを読み込んでるだけ
もしheadrer.phpに「aaaaa」とあったら
echo aaaaa;したと同じこと
そのまま取り込む、持ってくるのと同じ
つまり、main→functionsとなり
そこで$titleをうけとり、
require_once("header.php");をすると、
読み込んだheader.phpで、そのまま$titleが使えるらしい。
遷移するんじゃなくて「取ってくる」だからこういう挙動が可能。
スコープの範囲はあくまで関数内である。
--->header.php
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title><?php echo $title; ?></title>
</head>
<body>