Require config not working in function php?

Require config not working in function php?

Let’s say you have the database connection “$connection” variable inside a config.php file.

You want to use require_once (‘config.php’); inside a function? You will get an error.

An easy fix is to do it like this.

1
2
3
4
5
6
7
8
require_once ('config.php');
 
function your_function($connection)
   {
       echo'hello';
   }
 
echo your_function($connection).

echo your_function($connection). will pass the $connection variable inside the function.
Just be sure that you have also $connection as $connection = mysqli_connect($db_address, $db_username, $db_password, $db_name) OR DIE (“Can’t connect to mysql”); inside config.php.

Same with variables that are outside your function like variables with mysqli_real_escape_string.

Add a comment: