Are you using Linux? Then it happens because of the sockets. If you specify 127.0.0.1 PHP uses TCP/IP communication. If you type in localhost it tries to communicate with the socket (mysql.sock). It is possible that MySQL is not configured to support them or your system is blocking PHP from accessing the socket file.
EDIT: Possibly related: mysql_connect (localhost / 127.0.0.1) slow on Windows platform