How to convert a string into number in PHP?

PHPServer Side ProgrammingProgramming

To convert a string into a number, the code is as follows−

Example

 Live Demo

<?php
   $str = "150";
   $num = (int)$str;
   echo "Number (Converted from String) = $num";
?>

Output

This will produce the following output−

Number (Converted from String) = 150

Example

Let us now see another example−

 Live Demo

<?php
   $str = "100.56";
   echo "String = $str";
   $num = floatval($str);
   echo "\nNumber (Converted from String) = $num";
?>

Output

This will produce the following output−

String = 100.56
Number (Converted from String) = 100.56
raja
Published on 26-Dec-2019 10:10:34
Advertisements