What are the recommended settings for utilizing ZLib functionality in PHP?

When utilizing ZLib functionality in PHP, it is recommended to set the compression level to a value between 0 (no compression) and 9 (maximum compression) for optimal performance. Additionally, it is important to properly handle errors and exceptions that may occur during compression or decompression operations.

<?php
// Set the compression level to 6 for a good balance between speed and compression ratio
$compressionLevel = 6;

// Compress data using ZLib compression
$compressedData = zlib_encode($data, ZLIB_ENCODING_DEFLATE, $compressionLevel);

if ($compressedData === false) {
    throw new Exception('Failed to compress data');
}

// Decompress data using ZLib decompression
$decompressedData = zlib_decode($compressedData);

if ($decompressedData === false) {
    throw new Exception('Failed to decompress data');
}
?>