What best practices should be followed when using mkdir function in PHP to ensure consistent behavior across different server environments?

When using the mkdir function in PHP to create directories, it's important to ensure consistent behavior across different server environments by setting the correct permissions and handling errors properly. To do this, you should use the bitwise OR operator (|) to combine the desired permissions with the umask value, and check for errors using the mkdir function's return value.

$dir = 'path/to/new/directory';
$permissions = 0755;
$umask = umask(0);
if (!mkdir($dir, $permissions | $umask, true) && !is_dir($dir)) {
    echo "Failed to create directory";
}
umask($umask);