Norse
Norse

Reputation: 5757

PHP: Include as a string without output buffering?

Say there's an if statement:

if (stripos($names, "jack") !== false || stripos($names, "bob") !== false) {
 echo "Jack or Bob found.";
}

I'd like to use this statement across multiple pages without having to edit the if statement in each page if I wanted to change the if parameters.

I've tried this:

$contents = file_get_contents('names.php');
if ($contents) {
 echo "Jack or Bob found.";
}

names.php
<?php
 $return_me = stripos($names, "jack") !== false || stripos($names, "bob") !== false;
 return $return_me;
?>

And it doesn't work. I'm trying to get this done without using output buffering because it screws up my entire script. Does anyone know a solution?

Upvotes: 3

Views: 231

Answers (1)

Smamatti
Smamatti

Reputation: 3931

I suggest you write a function for this in a utility file you can include.

utils.php.inc

function check_found_user($names) {
    return (stripos($names, "jack") !== false || stripos($names, "bob") !== false);
}

Your other source code files

require_once('utils.php.inc');

if ( check_found_user($names) ) {
    echo "Jack or Bob found.";
}

Upvotes: 2

Related Questions