maybe a very simple question, however I have no idea what I do wrong, maybe someone could help me?
There is a GET-variable that contains the year and I want to check if it is actually a valid format and also not in the past, meaning the current year or later. What I did till now was:
$current_year=date('Y',$today);
(is_int(intval($_GET['year'])) AND $_GET['year']>$current_year) ? $year=$_GET['year'] : $year=$current_year;
The function intval() returns as it should 0 or 1 as stated in the php.net manual, if the GET-var is a not an integer, e.g. string “abc”.
However, if $_GET[‘year’] contains “abc” which is then converted into 0 and should not be > 2012, $year is not 2012!?
Any hint would be highly appreciated and thank you in advance!
EDIT:
Thank you for all your great answers! I’ve tried to consider all of them, and this works now perfectly for me:
if (ctype_digit($_GET['year'])) { ($_GET['year'] > $current_year && $_GET['year']<=($current_year+2)) ? $year=$_GET['year'] : $year=$current_year;} else {$year=$current_year;}
As I only need the next 2 years in the future, I added this… thank you for the hint to check if it is only 4 digits, deceze! 🙂
intvalwill mangle anything into anint, even if just into0, which meansis_intis consequently alwaystrue.is_int(intval())is therefore pointless.$_GETvalues will always be strings, even if they only contain numbers. Therefore you probably want to check usingctype_digit, which tells you whether all characters in the string are numbers. So your check should look something like: