Skip to content

Instantly share code, notes, and snippets.

@bxt
Created February 27, 2011 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bxt/846183 to your computer and use it in GitHub Desktop.
Save bxt/846183 to your computer and use it in GitHub Desktop.
Examining various ways to convert a string to a number in PHP (cast, intval, *1)
<?php
$strs=array('1','10','10a10','a12','a','10E3','4.6','4,6','4%6');
foreach ($strs as $str) {
$int=(int)$str;
$by1=$str*1;
$iv=intval($str);
echo "$str\tint:\t$int\tby1:\t$by1\tintval:\t$iv\n";
}
/*
Output:
1 int: 1 by1: 1 intval: 1
10 int: 10 by1: 10 intval: 10
10a10 int: 10 by1: 10 intval: 10
a12 int: 0 by1: 0 intval: 0
a int: 0 by1: 0 intval: 0
10E3 int: 10 by1: 10000 intval: 10
4.6 int: 4 by1: 4.6 intval: 4
4,6 int: 4 by1: 4 intval: 4
4%6 int: 4 by1: 4 intval: 4
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment