Let’s create a very simple example file:
index.php:
<?php
$connection = mysqli_connect('localhost', 'user', 'pass', 'db');
// connection established successfully
echo 'doing something here for like 500 lines of code';
include('functions.php');
echo countCTR(12, 15);
?>
Now, the problem is in functions.php:
<?php
function countCTR($input1, $input2){
/* in this function I need my database connection ($connection)
which was established already in index.php before this
file was included. however, the $connection isn't open
inside this function. Do I have to execute mysqli_connect
inside every function I define?
*/
}
?>
You shouldn’t.
Just use
globalkeyword.