In this tutorial, we are going to see how to convert a string to an integer in PHP. As we know, PHP does not support explicit type definition in variable declaration. However, you can force a variable to be evaluated as a certain type using Type-Casting.
Let’s try the following example to understand how this works:
How to convert a string to an integer in PHP
<?php $nbr = "5.21"; // Cast to a float $float = (float)$nbr; echo $float; // Cast to an integer $int = (int)$nbr; echo $int; ?>
You can use (int) or (integer) to convert a variable to an integer, use (float) or (double) to convert a variable to float. In the same way, you can use (string) to convert a variable to a string, and so on.
MCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More