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 Quote Link to comment 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) {} Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
trq Posted August 5, 2007 Share Posted August 5, 2007 <?php $str = "12"; $var = (int) $str; ?> Quote Link to comment 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.