How can typecasting or intval() be used to convert a string value to an integer in PHP?

To convert a string value to an integer in PHP, you can use typecasting or the intval() function. Typecasting involves explicitly specifying the variable type, while intval() converts a string to an integer. Both methods can be used to ensure that a string value is treated as an integer in PHP.

// Using typecasting
$stringValue = "42";
$intValue = (int)$stringValue;

// Using intval() function
$stringValue = "42";
$intValue = intval($stringValue);