A relatively simple change might be to change the “return” to a variable assignment.
You would need to think of what kind of value you wanted it to have based on how the query worked before you went to use it. eg, empty string, null, array, etc. But I’m guessing you already have that handled in the “results” file.
Or you could wrap it in a function that returned a value then do something like
include 'thefilewiththefunction.php';
$result = thefunction();
I guess as long as what you’re doing works that’s good enough. When it gets to be a problem you’ll know it.
What kind of global scope variables is your current way causing?
I really dislike the idea of putting a variable assignment in an included file (especially one that would be called repeatedly in the middle of a file) and just assuming it exists.
Readability plummets, and the chance of error goes up.
Some people really like PHP and some really dislike it. Most developers that really like PHP do not have experience with languages such as C, C++ and Java (there are many others). PHP supports good development but it also makes it easy to make programs difficult to understand.
The include statement in PHP is very different from the include statement in C and C++.
Using the simple sample provided here, it is very difficult to explain why an include statement in PHP might be a problem. If the sample were more complicated with nested levels of includes and with many variables used without an easy way not know what is where and why then it can become what some programmers call spaghetti. I was a programmer back in the days when gotos were common. It is difficult to explain why a goto is bad when you have just one. But if you are a maintenance programmer that must fix or modify a program written by someone else and modified many times by many programmers then you will learn what a mess they can make.
So it partially depends who the program is written for. If it is your program and no one else will touch it then you can do things the way you want to. If you are developing the program for a client then it is better to keep things as clean as possible. You should make the software robust enough that further modifications can be made with minimal additional complications. So even if you are programming for yourself it is better to develop skills that are good for others too.
There is a term called Side effect that is relevant here. Side effects should be avoided and when they exist they should be documented. Putting code in a function tends to limit the scope of variables and therefore reduces side effects.
The latest PHP version 7.?? has introduced many new features which drastically improve performance and the language is now becoming very similar to a compiled C program.
To achieve the enhanced performance it is necessary to declare strict validation, state variable types, function parameters and function return values, etc
Failure to implement the new features will reduce performance due to the compiler having to guess at the implied type and return the nearest match:
Unfortunately PHP does not enforce any of the new features and they must be added… this is because backward compatibility has been maintained to ensure old scripts will require very few modifications in order to use the latest PHP versions.
Personally I endeavour to use all the new PHP features because it is similar to compiling C programs… which “fail fast” if there are any doubtful inconsistencies.
[/off-topic]
Yes this is getting off-topic so I will just say that PHP is certainly being improved and provides more capability for good programmers such as you to write good programs.