简体   繁体   中英

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. I have localhost pointing to a Development folder in wwwroot folder. Inside the dev folder, I have one index.php, a global include folder and multiple mini php sites in more folders.

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; you'd use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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