Tag Archives: PHP

PHP Line Counter Script

Want to know how many lines of code you wrote? Put this php script in your path and run it on the root directory of your php project and it will count the number of lines in all the *.php and *.inc files. Also takes an optional -html command line argument if you want to count html files too. Very easy to modify if you want to count other types of files like *.js and *.cpp.

<?php
$lines = 0;
$total_lines = 0;
$verbose = 0;
$file_type_html = 0;

function rglob($pattern='*', $flags=0,$path='')
{
	$paths=glob($path.'*',GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
	$files=glob($path.$pattern, $flags);
	foreach($paths as $path)
	{
		$files=array_merge($files,rglob($pattern, $flags, $path));
	}
	return $files;
}

// check for command line args
if($argc > 1){
	if(in_array($argv[1],array('--help','-help','-h','-?'))){
		echo "-v	verbose\n";
		echo "-html	count html files\n";
		exit(0);
	}
	if(array_search('-v',$argv))
		$verbose = 1;
	if(array_search('-html',$argv))
		$file_type_html = 1;
}

$file_array_php = rglob('*.php');
$file_array_incs = rglob('*.inc');
if($file_type_html)
	$file_array_html = rglob('*.htm*'); // catch *.html and *.htm

if($verbose){
	var_export($file_array_php);
	var_export($file_array_incs);
	if($file_type_html)
		var_export($file_array_html);
}

foreach($file_array_php as $file)
{
	$lines = count(file($file));
	$total_lines += $lines;
	if($verbose)
	{
		echo "\n";
		echo "$lines in the file $file";
	}
}

foreach($file_array_incs as $file)
{
	$lines = count(file($file));
	$total_lines += $lines;
	if($verbose)
	{
		echo "\n";
		echo "$lines in the file $file";
	}
}

if($file_type_html)
{
	foreach($file_array_html as $file)
	{
		$lines = count(file($file));
		$total_lines += $lines;
		if($verbose)
		{
			echo "\n";
			echo "$lines in the file $file";
		}
	}
}
echo "\n\n\n";
echo "There are a total of $total_lines lines\n";

?>

PHP LAMP/WAMP File Paths

I came across some PHP code I wrote way back in 2006. At the time I was doing lots of file access on both Windows and Linux with the same code and was getting sick of fixing ‘/’ vs. ‘\\’ porting issues. Its pretty straight forward simple code but thought it might be useful to others. Basically I’m just wrapping the PHP file access methods with my own that always take a Linux ‘/’ path separator and if the method determines its running on Windows it automatically converts the path to use ‘\\’.

function isWindows()
{
	$os = php_uname('s');
	return strstr($os, 'Windows');
}

function convertPath($LinuxPathName)
{
	if(isWindows())
	{
		$basename = getcwd();
		$LinuxPathName = str_replace('/','\\',$LinuxPathName);
		$LinuxPathName = $basename . $LinuxPathName;
	}
	return $LinuxPathName;
}

/**
* my_fopen
*
* @param string $LinuxPathName Use a pathname with linux '/' convention
* @param string $Permissions Use to specify file permission, example '+w'
* @return int File handler descriptor
*/
function my_fopen($LinuxPathName, $Permissions)
{
	$ConvertedPathName = convertPath($LinuxPathName);
	$handle = fopen($ConvertedPathName, $Permissions);
	return $handle;
}

/**
* my_file_exists
*
* @param string $LinuxPathName Use a pathname with linux '/' convention
* @return bool
*/
function my_file_exists($LinuxPathName)
{
	$ConvertedPathName = convertPath($LinuxPathName);
	return file_exists($ConvertedPathName);
}

/**
* my_filesize
*
* @param LinuxPathName Use a pathname with linux '/' convention
* @return int
*/
function my_filesize($LinuxPathName)
{
	$ConvertedPathName = convertPath($LinuxPathName);
	return filesize($ConvertedPathName);
}