Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

serialize()

Encode a PHP value into a binary-safe string retaining types and array keys.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

serialize(mixed $value): string

Encode a PHP value into a binary-safe string retaining types and array keys. String lengths in this format count bytes. Objects can customize their data using __serialize(). Store the result in a byte-preserving field when it may contain null bytes. A resource becomes i:0; and a Closure raises Exception.

Signature

serialize(mixed $value): string

Example output

a:2:{s:2:"id";s:3:"007";s:2:"ok";b:1;}
i:0;
Exception

Examples

<?php
echo serialize(["id"=>"007","ok"=>true]);
echo "\n",serialize(STDIN),"\n"; try { serialize(fn()=>1); } catch (Exception $e) { echo get_class($e); }

Result

a:2:{s:2:"id";s:3:"007";s:2:"ok";b:1;}
i:0;
Exception

Sources