Install PHP Composer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

if desired move (or use install arg) to put binary into path, e.g., mv composer.phar /usr/local/bin/composer

see: https://getcomposer.org/download/
and: https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx

PHP Redis with socket connection

if you are using Redis on a single server and/or want the web server at the same location to use sockets instead of TCP, update your redis config (redis.conf) to include:

unixsocket /var/run/redis/redis.sock

(you may be able to just uncomment that line to use the default)

You will need to make sure the socket connection has the correct permissions for the apache/php service. You can set the “unixsocketperm” attribute in redis.conf and make sure the socket location is appropriate for your apache/php set-up, or you can add a line in your redis start-up script to update the permissions, something like this:

chown :apache $SOCK && chmod g+w $SOCK

I’m not sure what the best practice is here, but I needed to do one the above to get it to work.

Prevent extension version issues using PECL with multiple PHP installs

Simply calling phpize, config and make to compile a new extension, even if you point directly to your php binary can still compile with the wrong version as can installing via PECL, even if you reference the pecl binary directly. Using the peclcmd.php script, however, you should be able to get it to load the correct libraries and install in the right place.

For instance, I recently installed PHP 5.4 in /usr/local/php5 to use instead of the native Mac PHP 5.3 install (via http://php-osx.liip.ch/) but I wanted to leave the Mac PHP in place so now I have multiple versions of PHP and can use either one, which is great for dev but can cause compile issues.

On one machine I was eventually able to manually download the extension via pecl download and then phpize and install by passing: ./configure --with-php-config=/usr/local/php5/bin/php-config to the build config but wasn’t able to figure out how to do this directly via pecl. When I next tried the same update to another machine, I remembered the peclcmd file and was able to simply use a single command: /usr/local/php5/bin/php /usr/local/php5/lib/php/peclcmd.php install {extension-name} and done.

Ymmv :)

While I’m comfortable compiling from scratch, it definitely can save a lot of headaches to use a packager if it gives you what you need and make updates a lot easier.

PHP debugging with FirePHP

FirePHP is a PHP debugging tool that is super helpful for, well, debugging PHP and much easier than doing a lot of print_r() to the page and easier access to Ajax results, etc. You can group results, show full arrays, objects, warnings, exceptions, and apply object filters to large objects to make then less unwieldy.

The “Insight” library is more full-featured and uses the “DeveloperCompanion” Firefox extension while FirePHP, which has been around for a while now works again with Firebug. I’ve used both but haven’t really had the patience to really discover the benefits of the Insight library. I tend to just prefer to use it like one would use Firebug’s console.log and have the messages appear in the same console panel in Firefox so your JS and PHP debugging is all in one place.

Unfortunately you can’t use Insight and FirePHP extensions at the same time, so switching requires browser restart. Hopefully the Insight library will be Firebug compatible at some point. One small, but annoying thing is that the Insight method pops a window in front of your active browser window causing it to lose focus. With a big enough monitor(s) it’s not a huge issue but if you don’t need the report every reload it can be a pain for small debugging tasks, so I’ve pretty much switched back to the old FirePHP tools.

Using the OO API, the syntax is nearly the same, so after swapping FF extensions, switching the code mostly involves a different init script which could look something like this:

$method = 'firephp';
// INSIGHT
if ($method == 'insight') {
	require_once('phar:///htdocs/lib/firephp.phar/FirePHP/Init.php');
	$inspector = FirePHP::to('request');  // page | controller | request
	$console = $inspector->console(); // 'Debug'
	// ... (also, make sure "insight keys are enabled) //
} else {
// FirePHP
	require_once('phar:///htdocs/lib/firephp.phar/FirePHPCore/FirePHP.class.php');
	$console = FirePHP::getInstance(true); 
	$console->setEnabled(true); // default = true -- if using in code that may get deployed, filter by IP
}

and put that in your bootstrap file or other include. The basic syntax is:

$console->log($var);

Output looks like:

Click here to see source for debug messages shown above

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php 
echo '<pre>';
require_once 'firePHPbootstrap.php';
if ($method == 'insight') {
	$console->show();
}
####################################
$msg = 'Hello Worldx';
$time_msg = $msg .' :: ' . time() . ' ';
//
$console->log($msg); #exit;
/**/
$var = array('i'=>10, 'j'=>20);
$console->log($var);
//
try {
  throw new Exception('Exception');
} catch(Exception $e) { 
	$console->log($e);
	$console->log('line: ' . $e->getLine());
}
 
class foo {
	public $one = 'one';
	public $two = 'two';
	public $three = 'three';
}
$bar = new foo();
 
$console->log($bar);
 
/**/
if ($method == 'insight') { 
	$console->group('group1')->open();
	$console->log('Group 1');    // Group Label
	$console->log('Group 1 message 1');
	$console->group('group1')->close();
} else {
	$console->group('Group 1');    // Group Label
	$console->log('Group 1 message 1');
	$console->groupEnd();
}
//
$console->log('Log Message');
$console->info('Info message');
$console->warn('Warning message');
$console->error('Error message');
//
$table = array();
$table[] = array('Row 1  Column 1', 'Row 1 Column 2');
$table[] = array('Row 2  Column 1', 'Row 2 Column 2');
$console->table('Sample Table', $table, array('Column 1', 'Column 2'));
 
$console->log($time_msg);
?>
<hr />
<?php echo $time_msg; ?>

PHP date function for calculating workdays in a date range

For a project I needed to get the number of business / working days in a date range. Based on a couple older ones found on PHP.net and SO, I put together an updated PHP function for this purpose. It uses the newer OO date structures for a bit more streamlined code, removed the less efficient strtotime functions and, more importantly, it allows for any days of the week to be considered non-working days.The last argument is non-working days, based on the %w date format where 0=Sunday. You can also pass in an array of holidays which is checked against the non-working days.

view full function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
	function getWorkingDays($startDate, $endDate, $holidays=null, $non_wk_day_txt='0,6') {
 
		$non_wk_days = explode(',',$non_wk_day_txt);
 
		$datetime1 = new DateTime($startDate);
		$datetime2 = new DateTime($endDate);
		$interval = $datetime1->diff($datetime2);
 
		$days = (int) $interval->days + 1;
 
		$num_full_weeks = floor($days / 7);
		$num_remaining_days = ($days % 7);
 
		// Sunday=0
		$dow = array(); // days of week
		$dow[0] = $datetime1->format('w');
 
		$rd = $num_remaining_days;
		for ($i=0; $i < $rd; $i++) {
                $d = ($dow[0] + $i) % 7;
			if ( in_array( $d, $non_wk_days) ) {
				$num_remaining_days--; 
			}
		}
 
		$workingDays = $num_full_weeks * (7-count($non_wk_days));
 
		if ($num_remaining_days > 0 )  {
			$workingDays += $num_remaining_days;
		}
 
		// subtract the holidays
		if (!empty($holidays)) {
			foreach ($holidays as $holiday) {
				$h_date = new DateTime($holiday);
 
				if ( !(in_array($h_date->format('w'), $non_wk_days)) ) {
					$workingDays--;
				}
			}
		}
		return $workingDays;
	}

« Previous Entries