Convert string to int or double

Quick and dirty to convert string to integer:
This will be zero if your string starts with an alpha character.

$myval = 0 + $mystring;

int intval ( mixed $var [, int $base ] )
Return an integer value of $var. The default base is 10.

echo intval('238'); //returns 238

float floatval ( mixed $var )

echo floatval('155.36'); //returns 155.36

Removing the currency symbol

$price = '$19.95';
echo floatval(str_replace('$', '', $price)); //returns 19.95




Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options