Skip to content

Instantly share code, notes, and snippets.

@s3b4stian
Last active November 5, 2017 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s3b4stian/774f063b82d361a226a413b014299d7f to your computer and use it in GitHub Desktop.
Save s3b4stian/774f063b82d361a226a413b014299d7f to your computer and use it in GitHub Desktop.
/**
* Convert number given as string to the proper type (int or float).
* https://secure.php.net/manual/en/language.types.type-juggling.php
*
* @param string $string Number as string ex '1.0', '0.9' etc
* @return int|float
*/
function strtonum(string $string)
{
if (fmod((float)$string, 1.0) === 0.0){
return (int) $string;
}
return (float) $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment