What is coercion in php?

1 answer

Answer

1037227

2026-05-13 01:00

+ Follow

Java
Java

Coercion, type coercion or type conversion is the process of altering the data type of a variable into another data type. PHP uses implicit type conversion (aka. Weak/Loose Typing) so it is not necessary to declare the type of a variable. For instance, the following will not produce any errors:

<?php

$foo = "A string";

$foo = 1;

$foo = array();

?>

You can however explicitly cast variables to a certain type:

<?php

$int = (int)4.5/2;

?>

In contrast, strongly typed languages such as C/C++/Java force you to declare the type of value a variable will hold and explicitly state the new type to perform a conversion; not doing so will result in a compiler error.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.