Jump to content

String to Value


Erazer

Recommended Posts

intval() will convert a variable to an integer, I think that's what you want.

$string = intval($string);

Edit: Also, floatval() will return a floating-point value.

[code=php:0]
$string = "45.9";
$int_string = intval($string);
$float_string = floatval($string);
echo ($int_string); // Will echo 45
echo ($float_string); // Will echo 45.9
[/code]
Link to comment
https://forums.phpfreaks.com/topic/18236-string-to-value/#findComment-78283
Share on other sites

Hi Jeremysr
thanks for the quick reply, I've tried the floatval() before but it doesnt seem to work if there is a operand in the string so

$string = "45.9";
$float_string = floatval($string);
echo ($float_string); // Will echo 45.9

but

$string = "45.9+10";
$float_string = floatval($string);
echo ($float_string); // Will also echo 45.9 and not 55.9

so i'm looking for something that will do the math in the string and out the result

thanks

Link to comment
https://forums.phpfreaks.com/topic/18236-string-to-value/#findComment-78302
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.