如何在PHP的Include函数中包含echo函数?
我想使用PHP包含函数包含一个文件。但是文件名必须动态生成 例如: 我们的URL为domain.com/test 我想包括test-sidebar.php文件 如果我们的URL为domain.com/test2 我想包括test2-sidebar.php文件 我尝试使用以下代码,但没有成功 请帮助我,提前谢谢如何在PHP的Include函数中包含echo函数?,php,html,Php,Html,我想使用PHP包含函数包含一个文件。但是文件名必须动态生成 例如: 我们的URL为domain.com/test 我想包括test-sidebar.php文件 如果我们的URL为domain.com/test2 我想包括test2-sidebar.php文件 我尝试使用以下代码,但没有成功 请帮助我,提前谢谢 $pageurl = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $editedpageurl = str_replace("/",
$pageurl = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$editedpageurl = str_replace("/", " ", $pageurl);
$pieces = explode(' ', $editedpageurl);
$last_word = array_pop($pieces);
$sidebar = $last_word."sidebar.php";
include_once('echo $sidebar;');
?>'
首先也是最重要的一点是单引号无法理解变量,并且您已经在使用php,那么您不需要在include_中编写echo
include_once($sidebar);
include_once($sidebar);
只需使用:
首先,让我们确保您拥有htaccess(或其他重写引擎),以便将domain.com/testURL重写为PHP处理文件,然后将这些语句更改为有效:
# HTACCESS
RewriteEngine on
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
# Change index.php with your file
PHP
只需删除echo和简单的quote
include_once($sidebar)代码>
# HTACCESS
RewriteEngine on
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
# Change index.php with your file
$sidebar = $last_word."-sidebar.php";
include_once($sidebar);