Disclaimer:
These pages about different languages / apis / best practices were mostly jotted down quckily and rarely corrected afterwards.
The languages / apis / best practices may have changed over time (e.g. the facebook api being a prime example), so what was documented as a good way to do something at the time might be outdated when you read it (some pages here are over 15 years old).
Just as a reminder.

PHP programming tips and tricks

Some notes on software development with PHP

Fake slow loading

E.g. when checking that the onload events in javascript are firing properly, you want to fake slow loading times of for example images
$file = 'images/ad.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
sleep(12);
readfile($file);

PhpUnit, solving global variable problem

runtest.php
/*
 * phpunit keep everything global,
 * meaning that if you have two files that test for example stuff in subpages/
 * you will get an error since it tries to redefined the no_render function
 * 
 * So, this script is a workaround, it executes phpunit on one file at the time
 * and present the results
 */

$mo = find_all_files('.');

$failed=array();
$succ=array();
$failedFiles=array();
$succFiles=array();
$failCnt=0;
$succCnt=0;

foreach($mo as $k => $file)
{
    echo "$file
";
    unset($output);
    exec("phpunit $file", $output, $returncode);
    
    if($returncode > 0 )
    {
        //failed
        array_push($failedFiles, $file);
        $failCnt++;
        $failed = array_merge($failed, $output);
        echo "---- F
";
    }
    else
    {
        $succCnt++;
        array_push($succFiles, $file);
        
        $succ = array_merge($succ, $output);
        echo "---- .
";
    }
    
}

foreach($succ as $k => $l)
{
  echo "$l
";
}
foreach($failed as $k => $l)
{
  echo "$l
";
}

echo "-----------------SUMMARY-----------------
";
echo "Succeded files:
";
foreach($succFiles as $k => $l)
{
  echo "$l
";
}
echo "------------------------------------
";
echo "Failed files:
";
foreach($failedFiles as $k => $l)
{
  echo "$l
";
}

echo "------------------------------------
";
echo "Failed: $failCnt Success: $succCnt
";
echo "------------------------------------
";

function find_all_files($dir)
{
  $root = scandir($dir);
  $result=array();
  foreach($root as $value)
  {
    $value = rtrim($value);

    if($value === '.' || $value === '..') {
      continue;
    }
    
    if(is_file("$dir/$value")) {
      
      if(0 == endsWith($value))
      {
        continue;
      }
      
      $result[]="$dir/$value";continue;
    }
    foreach(find_all_files("$dir/$value") as $value)
    {
      $result[]=$value;
    }
  }
  return $result;
}

function endsWith($haystack)
{
    return preg_match("/Test\.php$/", $haystack); 
}

Unit testing - SimpleTest

How to execute SimpleTest in subdirs
$test = new TestSuite('All file tests');

$dirs = array(
'classes/dir',
'connect',
'include',
'include/db',
'include/util',
'refactor/include'
);
global $configProd;
foreach ($dirs as $aDir) {
  foreach(glob('/php/unit/tests/'.$aDir.'/*Test.php') as $aFile) {

    if(! $configProd){
      echo "$aFile
"; } $test->addFile($aFile); } } $test->run(new HtmlReporter());

Profiling in PHP

Install xdebug sudo pecl install xdebug

Add to php.ini
xdebug.profiler_enable=On
xdebug.profiler_output_dir=/tmp/traces
extension=xdebug.so

More programming related pages

Workflow: Release process
Workflow: Bug tracking
Teambox (Redbooth) - Mantis connector
Design Patterns
Git & Github
Go / Golang
CVS
CVS backup script
Distribution process
Installation script
Java Server Faces
Facelets
jibx
jBoss
jBpm
Perl tips
Perl links
PostgreSQL
Python / py2app
Shell scripts
Xslt
Node.js
Facebook / Opengraph
PHP developer notes
Redbooth API through php
Website optimization
jqTableKit demo
Javascript / html / css links