PHP on OSX: Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’

If you’re running OSX and you can connect to the MySQL instance running on your localhost but when you try to connect through PHP you get this error:
PHP on OSX: Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’
It’s because PHP is looking in the wrong location for your mysql.sock file. PHP is looking in
/var/mysql/mysql.sock
but in OSX it’s here:
/tmp/mysql.sock
You can fix it with a symlink but the more elegant solution is to edit a line in your php.ini file so it reads thusly,
mysql.default_socket = /tmp/mysql.sock