0

OK so I have tried to research why this is not working, it is beyond me now. So please forgive me.

I have a PHP Document called include.php that has 2 lines in it to require certain files.

require('/srv/testwebsite.com/public/htdocs/includes/defs.php');
require_once('/srv/differenttestwebsite.com/public/htdocs/includes/functions.php');

I then require this doc on another PHP script at the very top so that I can use the Functions created within functions.php and the definitions and global arrays within the defs.php

require('/srv/crm.testweb.com/public/htdocs/includes/include.php');

Some of the arrays in defs are called $acpDropdowns and are setup as:

$acpDropdowns['abc'] = array('some'=>'thing','like'=>'this');
$acpDropdowns['xyz'] = array('this'=>'that','etc'=>'etc');

In my functions I have a function called openWizard and its setup like this:

function openWizard($action,$postTo,$id,$user,$update)
{
    global $acpDropdowns;
    ......
}

When I use the $acpDropdown arrays within this function nothing is populated. To test this I entered:

echo "<pre>".$acpDropdowns."</pre>";    
function openWizard($action,$postTo,$id,$user,$update)
{
    global $acpDropdowns;
    if(!$acpDropdowns)
    {
        echo "No Dropdowns";
    }
    .....
}

What this resulted in was when I loaded my page and included the include.php file was a full array of the $acpDropdowns as expected and then when I called the function I got "No Dropdowns"

enter image description here

I think I have given as much as possible, I have also run

if(!$acpDropdowns)
{
    $included = get_included_files();
    echo "<pre>{$included}</pre>";
}

And this output shows that the defs.php file is actually included!

Daryl Goode
  • 91
  • 2
  • 12
  • how/where are you calling the `openWizard()` function? anything from error reporting? – Funk Forty Niner Oct 11 '16 at 15:15
  • 1
    Are any of the `require` inside a function? – AbraCadaver Oct 11 '16 at 15:17
  • and for the `get_included_files()` function http://php.net/manual/en/function.get-included-files.php are you using it as per the manual? – Funk Forty Niner Oct 11 '16 at 15:18
  • The `openWizard()` function is being called from a custom module on the website. The module consists of the following code: `'; openWizardDebt(); echo '
    ';`
    – Daryl Goode Oct 11 '16 at 15:22
  • @Fred-ii- You can see how im using the `get_included_files();` as I have explained this in my question `if(!$acpDropdowns) { $included = get_included_files(); echo "
    {$included}
    "; }`
    – Daryl Goode Oct 11 '16 at 15:24
  • I see `testwebsite.com` and `differenttestwebsite.com` - are you trying to pull files from 2 different sites? If so, one or both may not be allowing for external use. If error reporting isn't set on one or all of them, see if anything comes from using it, if you're not doing so already http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Oct 11 '16 at 15:24
  • I am trying to pull from 2 different sites yes. But both sites are on same server and the include.php, functions.php and defs.php work fine on a separate domain (3rd site) still on the same server. I have error reporting set to max and display errors turned on and there is nothing. – Daryl Goode Oct 11 '16 at 15:30
  • The only way a variable seemingly declared as `global` is not accessible via `global` is that it has been `include`d within a function somewhere within the chain, which gives it function scope. To ensure it is always actually `global`, *declare* it as `global` when creating it. Or better: use `return` and explicit assignment instead of implicit availability. – deceze Oct 11 '16 at 15:37
  • If I type `echo "
    {$acpDropdowns}
    ";` **Directly** before `function openWizard()` and go the webpage, or any webpage that infact has functions.php called including the page im having this error on, the full $acpDropdowns array works a treat. Its just when its then called within that function, that it does not work. And again, it only does not work for this specific page. It does however work If I re include `function openWizard() { global $acpDropdowns; if(!$acpDropdowns) { include('/srv/cdn.uk-debtcollection.com/public/htdocs/php/include.php'); } }`
    – Daryl Goode Oct 11 '16 at 15:45
  • @deceze I fail to see how this is a duplicate a the moment. NOWHERE in my code have I entered $acpDropdowns = "something" let alone within a function. every single instance of $acpDropdowns is `$acpDropdowns['name_of_array'] = array (xxxxx)` – Daryl Goode Oct 11 '16 at 15:51
  • This question does not have an answer as the post states... @deceze I have read, and re read your reference question. And again I state and confirm that I have not "overwrittin" `$acpDropdowns` within a function anywhere and that it works fine with every other implementation I have used it with. It just seems to be this when this function is called the $acpDropdowns resets its self. Although I can call it before, and after the function and it will work as expected?! – Daryl Goode Oct 11 '16 at 16:12
  • Can you directly address the topic which has been raised several times now in the comments: is there any other function involved?! Specifically, are the `include` statements, *any* include statements anywhere, inside a function? – deceze Oct 11 '16 at 17:11
  • There are no other functions involved, the only function called is `openWizard` I thought I had answered this sorry.. I have checked and there are no `include` statements within any function within functions.php – Daryl Goode Oct 11 '16 at 18:32
  • Can you include a *complete* example we could copy and paste as is (make it simple, use short relative file names we don't have to edit) that reproduces this exact behavior? – deceze Oct 11 '16 at 18:35
  • I have tried to replicate, however I cannot. The module is within Wordpress and it seems that maybe a wordpress file is blocking it potentially. If I just use the code above standalone then it all works fine. – Daryl Goode Oct 12 '16 at 08:36
  • So my comment above holds: the only way this will behave as described is if your initial file is being included **within a function**, which makes total sense if it's running within Wordpress. Again, either ensure that `$acpDropdowns` is really global by **declaring** it with `global` in defs.php, or, better, use `return $acpDropdowns` in defs.php and `$acp = require 'defs.php'` to actually control what scope the variable will be in. – deceze Oct 12 '16 at 08:38

0 Answers0