User Tools

Site Tools


php

PHP

Useful functions

Format filesize human

function format_filesize($bytes, $binary = true, $decimals = 2)
{
	$b = $binary ? 1024 : 1000;
	if ($bytes < $b) {
		return number_format($bytes) . ' Byte';
	}
 
	$s = 'KMGTPEZY';
	$e = (int) floor(log($bytes, $b));
	$v = $bytes / pow($b, $e);
	$t = sprintf(' %s%sB', $s{$e - 1}, $binary ? 'i' : '');
 
	return number_format($v, $decimals) . $t;
}
$tests = array(0, 1, 999, 1000, 1024, 54321, 1024*1024 - 100, 6902382592, 1208925819614629174706176);
 
foreach ($tests as $t) {
	printf('<p>[%s] => %s / %s</p>', $t, format_filesize($t), format_filesize($t, false));
}

Format seconds to words

Example output: 47 days, 22 hours, 34 min, 30 sec

function seconds_to_words($seconds)
{
	// number of days
	$days=(int)($seconds/86400);
 
	// number of hours
	$hours = (int)(($seconds-($days*86400))/3600);
 
	// number of mins
	$mins = (int)(($seconds-$days*86400-$hours*3600)/60);
 
	// number of seconds
	$secs = (int)($seconds - ($days*86400)-($hours*3600)-($mins*60));
 
	return sprintf("%d %s, %d hours, %d min, %d sec", $days, $days > 1 ? 'days' : 'day', $hours, $mins, $secs);
}

Debugging

Dump all variables/functions

// dump a variable/array
var_dump($_REQUEST);
print_r($GLOBALS);
 
// dump all variables
print_r(array_keys(get_defined_vars()));
 
// dump user defined functions
$arr = get_defined_functions();
print_r($arr['user']);

Turn on error reporting

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);

In order to see parse errors as well, put these into .htaccess:

.htaccess
php_flag display_errors on
 
# E_ALL | E_STRICT
php_value error_reporting 32767

Error logging (into file)

error_reporting(E_ALL | E_STRICT);
ini_set('log_errors', 'On');
ini_set('error_log', '/var/www/site/error.log');

xdebug (with eclipse and firefox/chrome)

Installation

sudo apt-get install autoconf #m4 automake

(assuming php has been previously compiled)

chmod -v a+x php-5.3.3/scripts/{phpize,php-config}
tar xvf xdebug-2.1.0.tar.gz
cd xdebug-2.1.0
../php-5.3.3/scripts/phpize
./configure --enable-xdebug --with-php-config=../php-5.3.3/scripts/php-config
make
sudo make install

Configuration

sudo vi /etc/php5/apache2/php.ini
> zend_extension = /usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so
sudo /etc/init.d/apache2 restart

Per directory setting: turn on xdebug remote debugging

.htaccess
> php_value xdebug.remote_enable On

Reverse port forward from remote machine to local machine

home$ ssh -N -R 9000:localhost:9000 user@server

Configure eclipse project like this: eclipse project settings for xdebug

Install Firefox-Addon “easy Xdebug”
https://addons.mozilla.org/en-US/firefox/addon/58688/

  • Toggle debug-mode with green bug in right corner
  • Open/refresh page to debug
  • Eclipse should be triggered → select Debug Perspective
  • Debug :)

Or Chrome-Extension “Xdebug helper”
https://chrome.google.com/extensions/detail/eadndfjplgieldjbigjakmdgkmoaaaoc

Turning off output buffering

// turn off apache2's gzip-support
@apache_setenv('no-gzip', 1);
 
// turn off PHP's internal gzip compression
@ini_set('zlib.output_compression', 0);
 
// setup output buffers
@ini_set('implicit_flush', 1);
 
for ($i = 0; $i < ob_get_level(); $i++)
    ob_end_flush();
 
ob_implicit_flush(1);
php.txt · Last modified: 2013-11-05 23:40 CET by dominik