How can typecasting be used to convert a string to an integer in PHP?

Typecasting in PHP can be used to convert a string to an integer by explicitly specifying the data type. This can be done by using the (int) or intval() function to convert the string to an integer. This is useful when you need to perform mathematical operations on a string that represents a number.

$string = "123";
$integer = (int)$string;
// or
$integer = intval($string);

echo $integer; // Output: 123