简体   繁体   中英

PHP: Set variable in to include file in 'if' statement

I am trying to set a variable in an if statement so that only when the if statement is true, the variable will be set and will be able to be used further down on the page.

Here's what I have tried:

if ($row[8] == 1) {
    echo 'Message here';
    ob_start();
    include('F164.php');
    $f164 = ob_get_clean();
}

as well as:

if ($row[8] == 1) {
    echo 'Message here';
    $f164 = include('F164.php');
}

new answer:

<?php
if ($example === 123) {
    ob_start();
    include 'file_with_output.php';
    $var = ob_get_contents();
    ob_end_clean();
}
?>

Just tried, this works for me.

If this does not work for you, please give an example content of the included file.


old answer:

I assume the value of the variable is in the F164.php file. You can assign it by returning the value in the included file:

F164.php looks like this:

 <?php $somevar = 'lolvalue'; return $somevar; ?> 

Code:

 if ($example == true) { $var = include 'F164.php'; } 

Means: If $example is true , $var will get the value 'lolvalue' .

Edit:

After reading your question again and again, i'm not sure if we mean the same thing. Still hope i could help.

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