gzencode()

gzencode() — Create a gzip compressed string.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

This function returns a compressed version of the input data compatible with the output of the gzip program.

Signature

gzencode(string $data, int $level = -1, int $encoding = ZLIB_ENCODING_GZIP): string|false

Parameters

ParameterTypeWhat it takes
$datastringThe data to encode.
$levelintThe level of compression. Can be given as 0 for no compression up to 9 for maximum compression. If not given, the default compression level will be the default compression level of the zlib library.
$encodingintThe encoding mode. Can be FORCE_GZIP (the default) or FORCE_DEFLATE.

Return value

The encoded string, or false if an error occurred.

Example

A runnable snippet:

$data = file_get_contents("bigfile.txt"); $gzdata = gzencode($data, 9); file_put_contents("bigfile.txt.gz", $gzdata);

See also

gzdecode(), gzdeflate(), gzinflate(), gzuncompress(), gzcompress()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page gzencode() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.