<pre>
<?php 
/*
 * intval and casting integers tests
 * 
 * Last Updated: 13. May 2010
 *  
 * @author hakre <http://hakre.wordpress.com/>
 */

$values = array(
	array('value' => 42                     , 'intval' => 42),
	array('value' => -42                    , 'intval' => -42),
	array('value' => 4.2                    , 'intval' => 4  ),
	array('value' => '42'                   , 'intval' => 42 ),
	array('value' => '+42'                  , 'intval' => 42 ),
	array('value' => '-42'                  , 'intval' => -42),
	array('value' => 042                    , 'intval' => 34        , 'notation' => '042' ), # 34
	array('value' => '042'                  , 'intval' => 42 ),
	array('value' => 1e10                   , 'intval' => 1410065408, 'notation' => '1e10'), # 10000000000
	array('value' => '1e10'                 , 'intval' => 1  ),
	array('value' => 0x1A                   , 'intval' => 26        , 'notation' => '0x1A'), # 26
	array('value' => 42000000               , 'intval' => 42000000),
	array('value' => 2147483647             , 'intval' => 2147483647),
	array('value' => 2147483648             , 'intval' => -2147483648),
	array('value' => 420000000000000000000  , 'intval' => 0         , 'notation' => '420000000000000000000'), # 4.2E+20
	array('value' => '420000000000000000000', 'intval' => 2147483647),
	array('value' => array()                , 'intval' => 0  ),
	array('value' => array('foo', 'bar')    , 'intval' => 1  ),
);

$tests  = array_keys($values);
$labels = array(0 => 'pass.', 1 => 'failed.', 2 => 'different.', 3 => 'error.' , 4 => 'skipped.');

printf ("PHP %s %s/%s - %s\n\n", PHP_VERSION, PHP_OS, PHP_SAPI, str_replace(' +0000', ' UTC ', gmdate('r')));

print "\n* TEST #1: Comparison of results intval() and casting to integer:\n\n";
print "#   INPUT                     EXPECTED      INTVAL()      CAST        RESULT\n";
print "----------------------------------------------------------------------------\n";
foreach ($tests as $test) {
	flush();
	$value    = $values[$test];	
	$input    = $value['value'];
	$notation = test_varnotation($value);
	$expected = $value['intval'];
	$intval   = intval($input);
	$castval  = (int) $input;
	$result   = 1 - (int) ($expected == $intval && $intval === $castval);
	$label    = $labels[$result];
	printf('#%-2s %-23s : %-11s / %-11s / %-11s  %s', $test, $notation, $expected, $intval, $castval, $label);
	
	print "\n";
}
print "\n";
flush();

print "\n* TEST #2: Comparison of results intval() and 0 + \$var:\n\n";
print "#   INPUT                     EXPECTED      INTVAL()      0+\$VAR      RESULT\n";
print "----------------------------------------------------------------------------\n";
foreach ($tests as $test) {
	flush();
	$value    = $values[$test];	
	$input    = $value['value'];
	$notation = test_varnotation($value);
	$expected = $value['intval'];
	$intval   = intval($input);
	if (is_array($input)) {
		$castval  = $labels[3];
		$label    = $labels[4];	
	} else {	
		$castval  = 0 + $input;
		$result   = 1 - (int) ($expected == $intval && $intval === $castval);
		$label    = $labels[$result * 2];
	}
	printf('#%-2s %-23s : %-11s / %-11s / %-11s  %s', $test, $notation, $expected, $intval, $castval, $label);
	
	print "\n";
}
print "\n";
flush();

print "\n* TEST #3: abs((int)\$val) vs. (int) abs(\$val) \n\n";
print "#   INPUT                     ABS((INT)\$val)   (INT)ABS(\$val)      RESULT\n";
print "----------------------------------------------------------------------------\n";
foreach ($tests as $test) {
	flush();
	$value    = $values[$test];	
	$input    = $value['value'];	
	$notation = test_varnotation($value);
	$absint   = abs((int) $input);
	$intabs   = (int) abs($input);
	$result   = 1 - (int) ($absint === $intabs);
	$label    = $labels[$result * 2];
	printf('#%-2s %-23s : %-14s / %-11s        %s', $test, $notation, $absint, $intabs, $label);	
	print "\n";
}
print "\n";
flush();

print "\n* TEST #4: abs((int)\$val) vs. absint(\$val) \n\n";
print "#   INPUT                     ABS((INT)\$val)   ABSINT(\$val)       RESULT\n";
print "----------------------------------------------------------------------------\n";
foreach ($tests as $test) {
	flush();
	$value    = $values[$test];	
	$input    = $value['value'];
	$notation = test_varnotation($value);
	$absint   = abs((int) $input);
	$intabs   = absint($input);
	$result   = 1 - (int) ($absint === $intabs);
	$label    = $labels[$result * 2];
	printf('#%-2s %-23s : %-14s / %-10s         %s', $test, $notation, $absint, $intabs, $label);	
	print "\n";
}
print "\n";
flush();

ini_set("max_execution_time", "300");
$iterations = 100000;

print "\n* TEST #5: Speed comparison (int)\$val  vs. intval(\$val) \n\n";
print "#   INPUT                     (INT)\$val        INTVAL(\$val)       RATIO\n";
print "----------------------------------------------------------------------------\n";
foreach ($tests as $test) {
	flush();
	$value    = $values[$test];	
	$input    = $value['value'];
	$notation = test_varnotation($value);
	
	$start = microtime(true);
	for ($i = 0; $i < $iterations; $i++) {
		$calc = (int) $input;
	}
	$stop = microtime(true);
	$timea = $stop - $start;

	$start = microtime(true);
	for ($i = 0; $i < $iterations; $i++) {
		$calc = intval($input);
	}
	$stop = microtime(true);
	$timeb = $stop - $start;
	
	
	$ratio = ($timeb / $timea) * 100;
	printf('#%-2s %-23s : %-12f   / %-12f         %3d%%', $test, $notation, $timea, $timeb, $ratio);	
	print "\n";
}
printf("(%s iterations each)\n\n", number_format($iterations));
flush();

print "\n* TEST #6: Speed comparison settype() vs. intval() \n\n";
print "#   INPUT                     SETTYPE()         INTVAL(\$val)       RATIO\n";
print "----------------------------------------------------------------------------\n";
foreach ($tests as $test) {
	flush();
	$value    = $values[$test];	
	$input    = $value['value'];
	$notation = test_varnotation($value);
	
	$start = microtime(true);
	for ($i = 0; $i < $iterations; $i++) {
		$calc = $input;
		$r    = settype( $calc, 'int' );
	}
	$stop = microtime(true);
	$timea = $stop - $start;

	$start = microtime(true);
	for ($i = 0; $i < $iterations; $i++) {
		$calc = $input;
		$calc = intval($input);
	}
	$stop = microtime(true);
	$timeb = $stop - $start;
	
	
	$ratio = ($timeb / $timea) * 100;
	printf('#%-2s %-23s : %-12f   / %-12f         %3d%%', $test, $notation, $timea, $timeb, $ratio);	
	print "\n";
}
printf("(%s iterations each)\n\n", number_format($iterations));
flush();

print "\n* TEST #7: Speed comparison abs((int)\$val) vs. absint(\$val) \n\n";
print "#   INPUT                     ABS((INT)\$val)   ABSINT(\$val)       RATIO\n";
print "----------------------------------------------------------------------------\n";
foreach ($tests as $test) {
	flush();
	$value    = $values[$test];	
	$input    = $value['value'];
	$notation = test_varnotation($value);
	
	$start = microtime(true);
	for ($i = 0; $i < $iterations; $i++) {
		$calc = abs((int) $input);
	}
	$stop = microtime(true);
	$timea = $stop - $start;

	$start = microtime(true);
	for ($i = 0; $i < $iterations; $i++) {
		$calc = absint($input);
	}
	$stop = microtime(true);
	$timeb = $stop - $start;
	
	
	$ratio = ($timeb / $timea) * 100;
	printf('#%-2s %-23s : %-12f   / %-12f         %3d%%', $test, $notation, $timea, $timeb, $ratio);	
	print "\n";
}
printf("(%s iterations each)\n\n", number_format($iterations));
flush(); 

print "\n* TEST #8: Speed comparison (int)\$val vs. 0 + \$val \n\n";
print "#   INPUT                     (INT)\$val        0+\$val             RATIO\n";
print "----------------------------------------------------------------------------\n";
foreach ($tests as $test) {
	flush();
	$value    = $values[$test];	
	$input    = $value['value'];
	$notation = test_varnotation($value);
	
	if (is_array($input)) {
		printf('#%-2s %-23s : %s', $test, $notation, $labels[4]);
		print "\n";
		continue;
	}
	
	$start = microtime(true);
	for ($i = 0; $i < $iterations; $i++) {
		$calc = (int) $input;
	}
	$stop = microtime(true);
	$timea = $stop - $start;

	$start = microtime(true);
	for ($i = 0; $i < $iterations; $i++) {
		$calc = 0 + $input;
	}
	$stop = microtime(true);
	$timeb = $stop - $start;	
	
	$ratio = ($timeb / $timea) * 100;
	printf('#%-2s %-23s : %-12f   / %-12f         %3d%%', $test, $notation, $timea, $timeb, $ratio);	
	print "\n";
}
printf("(%s iterations each)\n\n", number_format($iterations));
flush(); 

print "</pre>\n";
return;

function test_varnotation(array $test) {
	if (isset($test['notation'])) {
		return $test['notation'];
	}
	$input = $test['value'];
	
	$notation = preg_replace('(\n|\s+)', ' ' , var_export($input, true));
	$notation = str_replace(array('array ( ',' 0 => ','  1 => ',', )'), array('array(', '',' ',')'), $notation);
	return $notation;
}

function absint( $maybeint ) {
	return abs( intval( $maybeint ) );
}

# This is the last line in the fil ?>