hasanatkazmi Posted August 5, 2007 Share Posted August 5, 2007 I want to change data type of some variable, if it would be C++. following would be the code int $var; $var=int($str); i have two questions, how to declare $var as int in PHP, secondly how to cast a variable like () in PHP Link to comment https://forums.phpfreaks.com/topic/63427-solved-changing-data-type/ Share on other sites More sharing options...
BlueSkyIS Posted August 5, 2007 Share Posted August 5, 2007 how to declare $var as int in PHP: you don't. secondly how to cast a variable like () in PHP: if you want the integer value of a string, use intval($string). If you need to determine if a value is a number, you can use is_numeric(). to test whether a value is an integer, I use if (intval($string) == $string) {} Link to comment https://forums.phpfreaks.com/topic/63427-solved-changing-data-type/#findComment-316103 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 Yeah PHP is a loosely typed language. All of the following is acceptable but would not work in c/c++ without some trickery. $var = 1; $var = 1.1; $var = "Bob"; If you tried to write that code in c/c++ you'd get some nasty errors. Link to comment https://forums.phpfreaks.com/topic/63427-solved-changing-data-type/#findComment-316111 Share on other sites More sharing options...
trq Posted August 5, 2007 Share Posted August 5, 2007 <?php $str = "12"; $var = (int) $str; ?> Link to comment https://forums.phpfreaks.com/topic/63427-solved-changing-data-type/#findComment-316115 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.