包括文件原因:"Cannot redeclare",不包括:"Call to undefined function"



我有三个文件位于同一文件夹中:header.phpprofile.php域名.php.

域名.php:

<?php
function returnDomainName(){
return 'http://localhost/wordpress/';
}
?>

header.php:

...
<?php include 'domain-name.php';?>
...
<?php echo '<a href="' . returnDomainName() . 'login">Sign in</a>';?> // Here I'm just using returnDomainName function, and it works. Nothing special
...
<body <?php body_class();?>> // wordpress function, which in our case calls profile.php

profile.php:

include 'domain-name.php'; // If I'm including this one, page don't even load, with 'cannot redeclare' error
if(array_key_exists('logout', $_POST)) { // When I press logout button without including domain-name, I get an error 'call to undefined function'
header('Location: ' . returnDomainName());
wp_logout();
exit();
}

当我试图在配置文件中包含donmain名称时,我得到了一个错误Fatal error: Cannot redeclare returnDomainName()

但如果我没有,我会得到这个Call to undefined function returnDomainName()

我怎样才能摆脱这个错误循环?

WordPress 5.4.1,PHP 7.4.3

您应该使用require_onceinclude_once而不是include

最新更新