Last updated October 10th, 2018 at 08:05 am
Sometimes when developing, it’s helpful to echo some diagnostic information to the screen. Here’s some simple diagnostic PHP I use for displaying all included files.
Define this function:
function diag_included_files( $sort = true, $echo = true ) { $inc_files = get_included_files(); if ( $sort ) { sort($inc_files); } if ( $echo ) { echo 'Included Files:<pre>' . print_r($inc_files, true) . '</pre>'; } else { return $inc_files; } }
The above function uses the native php get_included_files function, which returns a numeric array consisting of all files included by the include, include_once, require, and require_once functions. The array also includes the script that calls the function.
If you want the list of included files to appear in the order in which they are included, call the function with the first argument set to false, like this:
diag_included_files(0); // $sort = false
The result will look something like this (the function is being called from the history-report.php file):
Included Files: Array ( [0] => /home/user/public_html/crm/history-report.php [1] => /home/user/public_html/crm/_includes/setup.php [2] => /home/user/public_html/crm/_includes/functions.php [3] => /home/user/public_html/crm/_includes/functions_utils.php [4] => /home/user/public_html/crm/_includes/functions_datetime.php [5] => /home/user/public_html/crm/_includes/class-nwb-form.php [6] => /home/user/public_html/crm/_includes/functions_validations.php [7] => /home/user/public_html/crm/_includes/functions_assembly.php [8] => /home/user/public_html/crm/_includes/functions_formdefs.php [9] => /home/user/public_html/crm/_includes/pagetop.php [10] => /home/user/public_html/crm/_includes/nav_main.php [11] => /home/user/public_html/crm/_includes/nav_hist.php )
Sometimes, especially if there are many included files and you’re searching for specific ones, it’s best to sort the files alphabetically. In that case your call would look like this…
diag_included_files(1); // arg #1 for $sort = true; could leave blank
…and the result will look like this:
Included Files: Array ( [0] => /home/user/public_html/crm/_includes/class-nwb-form.php [1] => /home/user/public_html/crm/_includes/functions.php [2] => /home/user/public_html/crm/_includes/functions_assembly.php [3] => /home/user/public_html/crm/_includes/functions_datetime.php [4] => /home/user/public_html/crm/_includes/functions_formdefs.php [5] => /home/user/public_html/crm/_includes/functions_utils.php [6] => /home/user/public_html/crm/_includes/functions_validations.php [7] => /home/user/public_html/crm/_includes/nav_hist.php [8] => /home/user/public_html/crm/_includes/nav_main.php [9] => /home/user/public_html/crm/_includes/pagetop.php [10] => /home/user/public_html/crm/_includes/setup.php [11] => /home/user/public_html/crm/history-report.php )
Note that in both cases, I left the second argument ( $echo ) at the default of true, so that the result of the function would be echoed to the screen rather than returned.
If you have questions or comments about this snippet of diagnostic PHP — or if you have some of your own diagnostic PHP to share — feel free to leave a comment.
Leave a Reply