为什么用 require 调用时看不到我的变量?
函数.php
<?php
function paginator(){
$links = array("index.php", "services.php", "content.php","contact_us.php" );
$trimslug = substr(strrchr($_SERVER['PHP_SELF'], "/"), 1);
foreach ($links as $key => $value) {
if ($value == $trimslug ) {
$GLOBALS['$page'] = $key;
}
}
$page = $GLOBALS['$page'];
$next = $page+1;
$previous = $page-1;
}
?>
内容.php
<?php
session_start();
require './functions.php';
paginator();
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pagination</title>
</head>
<body>
<h2>Now on Page : <?php echo $page?></h2>
<a href="<?php echo $links[$next] ?>" >Next</a>
<br><br><br>
<a href="<?php echo $links[$previous]?>" >Previous</a>
<br>
</body>
</html>
我希望能够在使用 require 函数时看到我的变量,因为这段代码将出现在每一页上。这可能是一个非常难以理解的概念,但我真的希望有人能正确地说明这个概念。
这似乎有效,谢谢大家。
<?php
$links = array("index.php", "services.php", "content.php","contact_us.php" );
$trimslug = substr(strrchr($_SERVER['PHP_SELF'], "/"), 1);
$page = null;
function paginator(){
global $links,$trimslug,$next,$previous,$page;
foreach ($links as $key => $value) {
if ($value == $trimslug ) {
// $GLOBALS['$page'] = $key;
$page = $key;
}
}
$next = $page+1;
$previous = $page-1;
}
?>