0

like my title says, whenever I try to use my variables from another php file, it doesn't work (Undefined variable). I did declare them in the file that I'm including. For example, I have this file called variables.php that have this in it:

<?php
$DEBUG = TRUE;
$mysqli = new mysqli("127.0.0.1", "root", "", "29185917-database");
$DEBUG_LOG_FILE = "../log";
?>

And then I have another file called debug.php that tries to use the variable 'DEBUG' but it cannot access it. Here is my debug.php file:

<?php
require_once 'variables.php';
function echo_debug(string $message)
{
    if($DEBUG) {
        echo $message;
    }
}
?>

Whenever I try to use my function echo_debug I get the error message : Undefined variable 'DEBUG'. Any help on this problem is appreciated :).

Trainsis
  • 3
  • 1
  • 3
    Functions have their own scope. The variable is accessible, just not from inside the function. In your function, right above your `if()`, try to add `global $DEBUG;` (Though it would be considerably better to pass `$DEBUG` as a parameter to the function instead of using global, but I have no idea how you are calling your function.) – GrumpyCrouton Oct 24 '18 at 13:46
  • you should pass the variable as a parameter. Also how do you call `echo_debug()`?? – Rotimi Oct 24 '18 at 13:48
  • I am calling my function in my index.php (home page) as echo_debug("comment that will help me debug in dev mode"); – Trainsis Oct 24 '18 at 13:49
  • 3
    you can alternatively use PHP's `define()`. Example: `define('DEBUG', true);` THen in the function check if `DEBUG` is defined – Rotimi Oct 24 '18 at 13:50

3 Answers3

1

Functions have their own scope. The variable is accessible, just not from inside the function.

You could pass $DEBUG as a parameter

function echo_debug(string $message, bool $DEBUG)

Then you would call it as

echo_debug("comment that will help me debug in dev mode", $DEBUG);

Another option is to declare DEBUG as a constant,

define('DEBUG', true);
$mysqli = new mysqli("127.0.0.1", "root", "", "29185917-database");
$DEBUG_LOG_FILE = "../log";

Then, in your function you would check for that constant:

function echo_debug(string $message) {
    if(DEBUG) { ... }
}

You could also use the global keyword, right above your if(), try to add global $DEBUG;.

require_once 'variables.php';
function echo_debug(string $message)
{
    global $DEBUG;
    if($DEBUG) { ... }
}

But generally the other two solutions are better, global variables are sometimes frowned upon.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
  • Thank you so much! Also, would there be any better alternatives because of the $DEBUG variable being redundant? – Trainsis Oct 24 '18 at 14:16
  • @Trainsis I'm not sure what you mean by it being redundant? – GrumpyCrouton Oct 24 '18 at 17:38
  • By needing to call the variable DEBUG, it every time you want to use the function echo_debug. It would be great to store the variable at one place and just change it there. – Trainsis Oct 24 '18 at 20:56
  • Are you talking about if you pass the variable when you call the function? My answer outlines 3 possible solutions to your problem, the first one is the only one that I would consider "redundant", because you'd have to pass the variable every time. – GrumpyCrouton Oct 24 '18 at 20:59
  • Yes I am talking about that – Trainsis Oct 24 '18 at 23:11
  • @Trainsis Right, so just use the 2nd method in my answer and you won't have that problem. – GrumpyCrouton Oct 25 '18 at 12:34
0

As per my comment, you can also use a constant and avoid the variable issue altogether.

<?php
define('DEBUG', true);
$mysqli = new mysqli("127.0.0.1", "root", "", "29185917-database");
$DEBUG_LOG_FILE = "../log";
?>

Then in the function check if the constant has been defined

<?php    
require_once 'variables.php';    
function echo_debug(string $message) {
    if (defined('DEBUG') && DEBUG === true) {
        echo $message;
    }
}    
?>
Rotimi
  • 4,783
  • 4
  • 18
  • 27
-1

do like this:

function echo_debug(string $message,$data)
{
    if($data === TRUE) {
        echo $message;
    }
}

call function:

require_once 'variables.php';
$message = "comment that will help me debug in dev mode";
$output = echo_debug($message,$DEBUG);
print_r($output);
Hiren Siddhpura
  • 189
  • 1
  • 8