How to cast string number into a float/int variable with PHP

290

Casting is a simple operation that can save many headaches. The code below show you how to cast a string into an float or integer with PHP.

// int example
$string_number = "72";
$float_number = (int) $string_number;

// float example
$string_number = "324.75";
$float_number = (float) $string_number;



2 comments ↓

If you found this post useful click the share this button. Contribute below by adding a comment, no registration is required.

  • Kadimi says on 02.07.12 at 5:48 pm comment #1

    Here is another one:

  • Kadimi says on 02.07.12 at 5:49 pm comment #2

    Here is another one:

    $x_string = “1.2e4″;
    $x_float = $x_string * 1;
    var_dump($x_float);
    // Outputs float(12000)

Leave a Comment