How can the octdec() function be used to avoid octal mode problems when setting file permissions in PHP?

When setting file permissions in PHP, using octal values can lead to unexpected behavior due to the way PHP interprets leading zeros. To avoid this issue, the octdec() function can be used to convert octal values to decimal before setting file permissions. This ensures that the permissions are applied correctly without any unintended consequences.

// Convert octal permissions to decimal using octdec()
$permissions = octdec('0644');

// Set file permissions using decimal value
chmod('file.txt', $permissions);