Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Type conversion

An explicit cast applies conversion rules for the target type.

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

Syntax

(int) $text; (bool) $text;

An explicit cast applies conversion rules for the target type. Casting a leading-numeric string to int takes its numeric prefix, and a nonnumeric string becomes zero. Boolean conversion follows different rules: among strings exactly "" and "0" become false. A cast alone cannot validate an input format.

echo json_encode([(int)"12x",(int)"blue",(bool)"0",(bool)"00",(bool)"false"]);
[12,0,false,true,true]

Examples

<?php
echo json_encode([(int)"12x",(int)"blue",(bool)"0",(bool)"00",(bool)"false"]);

Result

[12,0,false,true,true]

Sources