machelix Posted January 20, 2011 Share Posted January 20, 2011 <?php $temp = "01"; $temp = (int)temp; echo $temp; ?> I am using above code to convert string to integer variable but i am getting incorrect output.. somehow casting chopping values in the string. for example, above code should give me output 01 but actual output is 0. any pointer? i am new to php... Thanks, Rajesh Quote Link to comment https://forums.phpfreaks.com/topic/225132-problem-with-casting-from-strings-to-integer/ Share on other sites More sharing options...
Maq Posted January 21, 2011 Share Posted January 21, 2011 Why are you using 01 and not 1? 01 is not an integer. Quote Link to comment https://forums.phpfreaks.com/topic/225132-problem-with-casting-from-strings-to-integer/#findComment-1162784 Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2011 Share Posted January 21, 2011 $temp = (int)temp; ^^^ You don't have a defined constant named temp, so php assumed that you meant - $temp = (int)'temp'; And, well, the (int) of the string 'temp' is zero. If you meant to use $temp, please proof read your code when it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/225132-problem-with-casting-from-strings-to-integer/#findComment-1162787 Share on other sites More sharing options...
machelix Posted January 21, 2011 Author Share Posted January 21, 2011 first of all, i am really sorry that i did not paint clear picture of my problem. I am getting value 01 as string after some string spilt function. Now i want to use this value as integer. //function body function converthours($temp) { $temp = (int)temp; echo $temp *60; } //function call <?php $temp = "01"; //temp is declared as string variable with value 01. in original code, its value return by another function converthours($temp); ?> @Maq Sorry but why 01 is not an integer? Quote Link to comment https://forums.phpfreaks.com/topic/225132-problem-with-casting-from-strings-to-integer/#findComment-1162877 Share on other sites More sharing options...
machelix Posted January 21, 2011 Author Share Posted January 21, 2011 Thanks guys... problem solved: $temp = (int) $ temp; $ was missing Quote Link to comment https://forums.phpfreaks.com/topic/225132-problem-with-casting-from-strings-to-integer/#findComment-1162879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.