Diagnostic PHP: Get All Included Files

Share this!

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 ) {
	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:
	[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:
    [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.

Share this!

About JeffCohan.com

The nSiteful Tech Blog (the official blog of nSiteful Web Builders, Inc. since January of 2013) is where I (Jeff Cohan) and (occasionally) associates will be posting articles of potential interest to like-minded techies, nSiteful clients who are playing active roles in the maintenance of their own Web sites and blogs, and pretty much anyone interested in how Web strategies and tools can help them reach their goals.

This entry was posted in Snippets and tagged , , by Jeff Cohan. Bookmark the permalink.

About Jeff Cohan

Jeff and his wife, Margie, are the proud parents of Sarah and Jake. Jeff is the founder, president and chief cook and bottle washer of nSiteful Web Builders, Inc., a Web development and Internet Consulting firm. In his spare time, Jeff builds Web sites and Web applications, plays guitar, putters around in his basement woodworking shop, mercilessly spoils his grandchildren, and creates videos from more than two decades of home movies. His current video project is an extended montage of people (mainly family members) asking him to stop filming them.

Leave a Reply

Your email address will not be published. Required fields are marked *