简体   繁体   English

PHP仅在调用变量时包含文件

[英]PHP only include file if variable is called

I figured it would be best to give an example of an issue I'm trying to resolve vs posting my code. 我认为最好给出一个我要解决的问题的示例,而不是发布我的代码。 (If you would still like the code let me know). (如果您仍然想要代码,请告诉我)。

I'm trying to create one folder and one index.php file with multiple different sites inside that have large differences. 我正在尝试创建一个文件夹和一个index.php文件,其中包含多个具有很大差异的不同站点。 I have localhost pointing to a Development folder in wwwroot folder. 我有本地主机指向wwwroot文件夹中的Development文件夹。 Inside the dev folder, I have one index.php, a global include folder and multiple mini php sites in more folders. 在dev文件夹中,我有一个index.php,一个全局include文件夹以及更多文件夹中的多个mini php站点。

I need to setup variables for each site that point to include files. 我需要为每个站点设置指向包含文件的变量。 Example: 例:

$html5 =  include '/global-includes/global-html5standard.php';
$email =  include '/global-includes/global-emailtemp.php';

Depending on what site I want to see on the index, I will call the specific variable. 根据我想在索引上看到的站点,我将调用特定变量。

echo $html5;

While having the other variables commented out. 同时将其他变量注释掉。

//echo $email;

The issue is the include pulls in the content regardless. 问题是无论如何都包含对内容的提取。 I'd like to make it only pull in if it is called. 我想使其仅在调用时才拉入。

You could change your variables to just the path name. 您可以将变量更改为仅路径名。

$html5 = '/global-includes/global-html5standard.php';

Then include the variable. 然后包括变量。 Instead of echo $html5; 而不是echo $html5; you'd use include( $html5 ); 您将使用include( $html5 );

You may want to use functions instead. 您可能要改用函数

function email() {
    include('/global-includes/global-emailtemp.php');
}

function html5() {
    include('/global-includes/global-html5standard.php');
}

You may also want to use include_once() so the files will only be included once no matter how many times you call it. 您可能还想使用include_once(),因此无论调用多少次,文件都只会被包含一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM