trojal.

(adventures in web programming.)

Recent Entries
Categories
Meta (2)
Test (3)
Random (1)
jQuery (1)
SVG (1)
Links
Info
Edit

Two and Two

-trojal @ 2009-Feb-12 18:12 UTC
Two and two equals:
2 + 2 = 4		2 & 2 = 2		2 && 2 = 1		
2 + '2' = 4		2 & '2' = 2		2 && '2' = 1		
2 + 'two' = 2		2 & 'two' = 0		2 && 'two' = 1		
'2' + 2 = 4		'2' & 2 = 2		'2' && 2 = 1		
'2' + '2' = 4		'2' & '2' = 2		'2' && '2' = 1		
'2' + 'two' = 2		'2' & 'two' = 0		'2' && 'two' = 1		
'two' + 2 = 2		'two' & 2 = 0		'two' && 2 = 1		
'two' + '2' = 2		'two' & '2' = 0		'two' && '2' = 1		
'two' + 'two' = 0	'two' & 'two' = two	'two' && 'two' = 1	
I overheard someone say "1 and 1 makes 2" in terms of who knows what, and... I decided to find the different ways to interpret it. This is just PHP code, so it treats (int) + (string) by parsing the string as an integer (leading numeric characters = number, else it takes the value of 0).
<?php
function add($arg1, $arg2){
  return $arg1+$arg2;
}
 
function bit($arg1, $arg2){
  return $arg1 & $arg2;
}
 
function compare($arg1, $arg2){
  return ($arg1 && $arg2);
}
 
$two = array("2"=>2, "'2'"=>'2', "'two'"=>'two');
$add = array("+"=>'add', "&"=>'bit', "&&"=>'compare');
 
foreach($two as $key1=>$arg1){
  foreach($two as $key2=>$arg2){
    foreach($add as $funckey=>$func){
      echo $key1 . " " . $funckey . " " . $key2 . " = " . $func($arg1, $arg2) .
        (($arg1=="two" && $arg2=="two")?"":"\t") . "\t";
    }
    echo "\n";
  }
}
?>