Interesting PHP Quirk w/require_once

12 Sep

So as many of you astute readers may have noticed, we use PHP on the backend. Nathan and I ran into a curious quirk today; if you do a require_once() in a function’s context, the variables set in the required file are not visible as globals once the function returns, even to functions in the required file.

The moral of the story? If you use globals in your includes, put all the require_once()s that you might need at the top of your PHP. Don’t try and be clever with conditional inclusion of your libraries.

Here’s a simple test case to reproduce:

$var = “foo”;
function f() {

global $var;



function a() {



4 Responses to “Interesting PHP Quirk w/require_once”

  1. Shtepsell May 25, 2011 at 6:38 am #

    -, . . , , , , , , . . . .
    .: (44) 233-03-96, (44) 332-60-75, (67) 947-19-49, . (67) 947-19-49

  2. investment ira September 1, 2014 at 10:19 pm #

    Pretty nice post. I just stumbled upon your weblog and wanted to say that I’ve truly enjoyed surfing around your blog posts.
    After all I’ll be subscribing to your rss feed and
    I hope you write again soon!

  3. kazrazuru March 4, 2015 at 2:40 am # +79185540950 (Viber,WhatsApp) Владимир

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: