Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

strval()

Obtain the string representation for output. true becomes "1", while false and null both become the empty string.

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

Syntax

strval(mixed $value): string

Obtain the string representation for output. true becomes "1", while false and null both become the empty string. An object with __toString can supply its own text. Converting an array does not serialize its elements. Arrays produce "Array" with E_WARNING. A plain object without __toString, such as stdClass, raises Error.

Signature

strval(mixed $value): string

Example output

[["1","","","0","sale","Array"],true,"Error"]

Examples

<?php
class Tag { public function __toString(): string { return "sale"; } }
$warning = null;
set_error_handler(function($level) use (&$warning) { $warning = $level === E_WARNING; return true; });
$texts = array_map("strval", [true,false,null,0,new Tag(),[1]]); restore_error_handler();
try { strval(new stdClass()); } catch (Error $e) { $failure = get_class($e); }
echo json_encode([$texts,$warning,$failure]);

Result

[["1","","","0","sale","Array"],true,"Error"]

Sources