What are some best practices for setting chmod permissions in a PHP environment?

When setting chmod permissions in a PHP environment, it is important to follow best practices to ensure security and proper functionality of your application. One common approach is to only grant the necessary permissions to files and directories, such as read, write, and execute permissions for the owner, and restrict permissions for others. Additionally, it is recommended to avoid setting permissions to 777 (full access for everyone) as it can pose a security risk.

// Set permissions for a file to 644 (owner can read and write, others can only read)
$file = 'example.txt';
chmod($file, 0644);