This is what Apache's namebased hosting is for, it's a much better setup than doing it with PHP, however, if you really want to do it in PHP, create a file with the following:
<?php
phpinfo();
?>
When you view this page on your server, it will dump information about the server. Search the resulting page for the domain name and you'll see which variables you need to query in php, then just use a simple if/else.
Andrew