TeddyKiller Posted April 12, 2010 Share Posted April 12, 2010 I have a Users DOB put into Age. If this Age is bigger than 18 (for example) then display certain information. $age would be the variable to hold the string of their age. How can I do this? I'm imagining simply doing if($age > 18) wouldn't do the job.. as $age is a string. . . Quote Link to comment https://forums.phpfreaks.com/topic/198298-how-can-i-check-if-a-string-is-bigger-than-x/ Share on other sites More sharing options...
the182guy Posted April 12, 2010 Share Posted April 12, 2010 Why not just cast $age as an integer like if((int)$age > 18) Quote Link to comment https://forums.phpfreaks.com/topic/198298-how-can-i-check-if-a-string-is-bigger-than-x/#findComment-1040441 Share on other sites More sharing options...
TeddyKiller Posted April 12, 2010 Author Share Posted April 12, 2010 I haven't used (int)'s before. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/198298-how-can-i-check-if-a-string-is-bigger-than-x/#findComment-1040443 Share on other sites More sharing options...
ignace Posted April 12, 2010 Share Posted April 12, 2010 use strcmp: Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal. Quote Link to comment https://forums.phpfreaks.com/topic/198298-how-can-i-check-if-a-string-is-bigger-than-x/#findComment-1040464 Share on other sites More sharing options...
Ken2k7 Posted April 12, 2010 Share Posted April 12, 2010 I haven't used (int)'s before. Thanks If you're not comfortable with that method of casting, use intval. I prefer to compare numbers as numbers rather than as strings with strcmp. Quote Link to comment https://forums.phpfreaks.com/topic/198298-how-can-i-check-if-a-string-is-bigger-than-x/#findComment-1040466 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.