1$stringVal = "12.06";
2$stringConvertedToFloat = floatval( $stringVal );
3// The floatval function will return the argument converted
4// to a float value if the value can be converted.
5// IF the value cannot be converted these are the values that will be
6// returned:
7// Empty Array: returns 0. eg: floatval([]);
8// Non-Empty Array: returns 1. eg: floatval(["ab", "12"])
9// String with a non-numeric value as the left most character: returns 0. eg: floatval("ab12")
10// String with one or more numeric values as the left most characters: returns those characters as a float. eg: floatval("12ab1") will return 12.
11// Oh the joys of php
1phpCopy<?php
2$mystring = "0.5674";
3echo("This float number is of string data type ");
4echo($mystring);
5echo("\n");
6$myfloat = floatval($mystring);
7echo("Now, this float number is of float data type ");
8echo($myfloat);
9?>
10
1phpCopy<?php
2$mystring = "0.5674";
3echo("This float number is of string data type ");
4echo($mystring);
5echo("\n");
6$myfloat = (float) $mystring;
7echo("Now, this float number is of float data type ");
8echo($myfloat);
9?>
10
1phpCopy<?php
2$mystring = "0.5674";
3echo("This float number is of string data type ");
4echo($mystring);
5echo("\n");
6$myfloat = number_format($mystring, 4);
7echo("Now, this float number is of float data type ");
8echo($myfloat);
9?>
10