RSS | Archive | Random | E-mail

About

Hi, I'm Christian Castelli, a 28 years old italian programmer located in Pisa (Italy). Here I post small snippets of code which can be useful in my work.

Links

Codepuzzling main site
Development site
ByteStrike italian blog
Follow me on Twitter

My Life Style

while(passion) {
  try {
    myLife.run();
  }catch(LifeExceptions) {  
    stronger++;
    continue;
   }
}

Following

3 September 09

[JS] Firefox debugging with Firebug Console API

I’ve found always useful using Bin-Blog’s dump function to explore the properties of an object or an array (like a PHP print_r) but now I’ve discovered that console.dir function does the same:

a = ['a', 1, true];
console.dir(a);
console.dir function will produce in output console this result:
>>> a = ['a', 1, true]; console.dir(a)
	
0	"a"
1	1
2	true

Comments (View)
8 July 09

[PHP] How to catch warnings inside a script

Put the case you’re doing a FTP upload of a file. If the transmission fails for whatever problem, you’ll receive a warning but your app won’t stop, you’ll only be able to see that an error occurred. So you can define an error handler to catch those warnings:

// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    switch ($errno) {
	case E__ERROR:   echo "FATAL ERROR ".
                "[line: $errline] $errstr in $errfile\n"; 
                 break;
	case E_WARNING:  echo "WARNING [line: $errline]". 
                $errstr." in $errfile\n"; 
                break;
	case E__NOTICE:  echo "NOTICE [line: $errline] $errstr ".
                in $errfile\n";  
                break;
	default: echo "UNKNOWN ERROR: [line: $errline] $errstr in $errfile\n"; 
    }

    /* Don't execute PHP internal error handler */
    return true;
}

set_error_handler("myErrorHandler");

$a = 0;    $b = 2;
$c = $b/$a;
echo $c;   // not outputted
This will raise a warning:
WARNING [line: 21] Division by zero in /var/www/temp/warning.php

Tags: php debugging
Comments (View)
7 July 09

[PHP] How much memory does your app consume?

Here it is a simple function to display how much memory your PHP script is consuming.

// debug function with time, memory consumption (MB) and optional custom message
public function getMemoryUsage($message="", $echo=1) 
{
 $mem_used = memory_get_usage(true)/ 1024 / 1024;
 $mem_peak = memory_get_peak_usage(true) / 1024 / 1024;
 preg_match("/(\d)+/",ini_get('memory_limit'),$matches);  // regexp, takes only digits
 $mem_limit = $matches[0];
 $percentual = round(($mem_used * 100) / $mem_limit,2);

 $res = date("H:i:s",time())." ".$message." ";		
 $res .= "[MEM_USED: $mem_used MB ($percentual%) - Peak: $mem_peak MB]\n";
 if($echo) echo $res;
 else return $res;
}

Tags: php debugging
Comments (View)
1 July 09

[JS] How to test if a variable is defined

// returns 1 if defined, 0 otherwise.
function isDefined(anObj) {
   switch(typeof(anObj)) {
    	case "string": if(anObj != "") return 1; else return 0; 
    	case "undefined": return 0;
    	case "object": if(anObj != null) return 1; else return 0;
    	default: return 0;
   }
}

Comments (View)
Themed by Hunson. Originally by Josh