Do you mean placing the php file in /var/www/ allows it to execute but placing it in /~myfolder/ does not?
If so, you need to configure /~myfolder/ as a virtual directory allowed to execute php.
php_admin_value engine Off
In your php5.conf (located in /etc/apache2/mods-enabled/)
Try changing Off to On and restart apache.