Mailinglist – php example code – part 6 – further small improvements

Article focus on small code improvements that can be extended by time. Our firs improvement is separation of database access constants into a appvars.php file. This file is included into a main code with require_once(); PHP function.

Better maintainability of of code is gained by separating all constants on one place. Then they can be invoked by include or require_once() PHP function. For further reading about diferences between these function, please visit as example this page.

File with defined constants, in our case database server access parameters is implemented in all .php files with require_once() statement.

appvars.php code

Next photo show content of mentioned file.

Example of appvars.php code

Example of changed parts of other pages follow.

<?php  // script for accessing database and first table structure establishement
require_once(appvars.php)// including variables for database
/* Attempt MySQL server connection. Assuming you are running MySQL
server with  (user ‚admin‘ with  password test*555) */
$dbc = mysqli_connect(DB_HOST, DB_USERDB_PWDB_NAME);
… next part omitted

Full application code of mailinglist can be obtained from github here.