Site Admin
Site Admin Founder of MeaningArticles
461 Views

PHP Convert String to Integer Example

Hello Dev.

Converting string to integer or numbers is extremely simple in PHP. PHP programming language supports the type conversion, and it offers numerous functions to convert a string into numbers.

Let us have a look on some the string to number conversion php functions with an example below.

The number_format() function in PHP is used to convert a string into an integer.

<?php 
    $int = "109.126"; 
      
    echo number_format($int), "\n"; 
    echo number_format($int, 2); 
?>

// Result: 109
// Result: 109.13

In the next step, we’ll take help of primitive types to convert a string into an integer, double or float.

<?php 
  
    $number = "30061987.0915"; 
      
    // int 
    echo (int)$number, "\n"; 
    
    // double 
    echo (double)$number, "\n";
      
    // float 
    echo (float)$number, "\n"; 
?>

// Result: 30061987
// Result: 30061987.0915
// Result: 30061987.0915

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.