Include and require statements

I am having some issues with include and require…

I have page1.php that calls function1

page2.php implements function1 but function1 also calls function2

page3.php implements function2

Keep receiving error redeclaring function1 previously declared in file2

What works for me is using the “_once” versions of include and require. TBH, I can understand the difference between include (don’t crash if it fails for some reason) and require (if it fails, STOP the code). But I can’t comprehend why there would need to be once and not once versions. The only guess I have is that the once is more load because it checks to see if it already exists which might be unnecessary, but I don’t imagine it would be a significant load.

3 Likes

The problem here is that regardless if you use _once or the regular include/ require, it will always be called. I ran into this issue when I first started PHP. but what solved my problem was that I had to wrap the function within an if statement. Here’s a snippet example

if(!function_exists('myFunctionName')) {

	function myFunctionName() {

		// Declare the function stuff here

	}

}
1 Like

If I had to guess you probably are trying to require the file from within your function.
In any event, sometimes it helps to create a simple test case:

// page1.php
require_once 'page2.php';

function1();

// page2.php
require_once 'page3.php';

function function1()
{
    echo "Function 1\n";
    function2();
}

// page3.php
function function2()
{
    echo "Function 2\n";
}

This is one of the reasons for using static class methods. Autoloading can replace all this explicit loading nonsense.

1 Like

How would i make variables from file1 (which are all from html form in that file) visible in file2.

I already have this statement…

 require 'file2.php'

They seem to be empty when i try to access them in file2…

I can pass all these variables as parameters in function1 and I know that would work but I am wondering why they are not available in file2

Are these global variables? And you trying to access them inside of your function? If so they need to be declared as globals inside of the function.
http://php.net/manual/en/language.variables.scope.php

But do yourself a favor and don’t do this. You are already on shaky grounds just using require statements. Globals tend to be very evil. Just pass your arguments as function parameters. If you have a bunch then pass them as an array.

This might be a bit overkill for now but might help in organizing your app:

1 Like

yes I use the array for resolve this…But now i have another problem…

file1.php has a variable at the top declared…

$myvar = "";

I am assigning it a value in the function that is inside file2.php but when I try to echo $myvar inside file1.php it is empty

So the variable is passed by reference first? As you read from the manual that @ahundiak linked:

Any variable used inside a function is by default limited to the local function scope

At least you should provide an example that shows the problem.

When a PHP file is included, the code within is run immediately “inline” where it is included.

The main reason you don’t always use “once” is because you might be including a partial. I had a site once where they had a PHP file that controlled an SVG master symbol library. So they would include() the PHP file and it would run logic to determine which SVG symbol is output. So this PHP file could be included multiple times in a page.

PHP can be included for any kind of output needed, not just function/class definition files.

Only if your PHP file contains ONLY functions and classes and such, should it be using “once”. And it should be included at the global scope, not from within a function or class.

In other words, this person using procedural style programming. Stuff should be included as needed within flow of how the code is processed. But if their includes are just function definitions, they should be included early in the app session. There should be no reason to need “once” if it’s programmed in the correct procedural way.

In any event, so you have page1.php, page2.php and page3.php. Are these loaded inline in the code or are they each independent endpoints? Like example.com/page1.php and example.com/page2.php?

If they are unique endpoints, then just put all your functions in some include file like helper-functions.php and include it from each file.
If they are not independent, a user only goes to example.com/page1.php and then this loads the other pages as needed, this is where you may get in trouble. But it can still be fixed by simply putting all your functions in a functions file and load it early so that the functions are all available to whatever file needs them.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.