skip to Main Content

I have a page with a variable and a function:

test.php

<?php
  $myVar = "myVar";
  
  function myFunc() {
    echo "myFunc()";
  }
?>

The first page that includes it, can see the variable value.

<?php
  echo "page1";
  require_once("include/test.php");
  print_r($myVar);

function GoTo2() {
  $urlSite = "page2.php";
  include($urlSite); 
  exit();
}

GoTo2();
?>

but the second page, cannot see the variable.

<?php
  echo "page2n";
  require_once("include/test.php");
  print_r($myVar);
?>

it generates PHP Warning: Undefined variable $myVar in .../page2.php on line 4

I cannot use require() in the second page, because it generates an error – PHP Fatal error: Cannot redeclare myFunc() (previously declared in .../include/test.php) in .../include/test.php ...

What is the proper php way that both pages can see the variable and also can use the function?

EDIT:

  • this is a simplified of my real pages. I just discovered that page2 cannot see the variable when the site was upgraded to php8, and I recieved all "Undefined variable" warnings.
  • page1 is a "dispatcher" page
  • suppose I inherited this code from another developper, what is the best solution to solve the problem?

2

Answers


  1. First problem is that the second include will not happen because it’s already included.

    Second problem is that you cannot use variables from outside the function. You need to either send the variable as a parameter in the function or you can make the variable to be global

    First solution:

    function GoTo2($myVar) {...

    Second solution:

    funtion GoTo2() { global $myVar; ...

    Login or Signup to reply.
  2. Since your real question is not "why is this happening", but rather "what is the proper way of doing it", then theoretically the whole scheme would need a rethink, as pointed out by @ADyson.

    Basically, you have that page1.php includes test.php and page2.php, which then also includes test.php again. But the logic of includes is that you should include files only once.

    It is not wrong to include variable definitions, like you would do, for instance, in the case of a configuration file. But then again: you include configurations only once; if it happens twice, it’s most probably a bug, or a misconception. The same applies with libraries of functions.

    There’s really no "only right way to do it". It would depend on what’s available to you, and what you intend. That being said, an ideal design could be to gather all configurations in one file; the functions gathered in one or more files, according to their specificity; and have a core file that include whichever files you need for the particular operation. For example, a structure like this:

    • /confs/display_config.php
    • /confs/parameters_config.php
    • /functions/encryption_routines.php
    • /functions/processing_routines.php
    • /functions/math_routines.php

    If you have many files to include, you could make a proxy configuration file:

    configuration.php

    <?php
    require_once('/confs/display_config.php');
    require_once('/confs/parameters_config.php');
    require_once('/functions/encryption_routines.php');
    require_once('/functions/processing_routines.php');
    require_once('/functions/math_routines.php');
    ?>
    

    Then, your page1.php would be:

    <?php
    require_once('configuration.php');
    // ... and other stuff
    ?>
    

    The concept of "sub-pages" (like a page2.php called within page1.php) is not necessarily wrong either, although it could reveal itself tricky: you’ll have to keep in mind that this piece of code is meant to always be called by another page, and presuppose all the configurations that have been done prior to call.

    So the thing, here, is that a given page (in your case, page2.php) ideally is not supposed to be called both directly and by another page (in your case, page1.php).

    Either page1.php and page2.php are both always called directly, and thus load separately their own configurations (page1.php includes test.php, and page2.php includes test.php), or page2.php is always loaded by page1.php, and then relies on it for its own configurations (page1.php includes test.php, then page2.php: here page2.php will have access to the configurations in test.php already loaded by page1.php).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search