Created
February 27, 2011 13:31
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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