简体   繁体   中英

php error with filter_input

I don't understand this error and also I have never used the "INPUT_GET, 'wk', FILTER_SANITIZE_STRING" before Could someone explain this or help fix this thanks.

error Notice: Use of undefined constant ‘wk’ - assumed '‘wk’' in /Applications/MAMP/htdocs/Calendar/test1.php on line 172

code

if(!filter_input(INPUT_GET, ‘wk’, FILTER_SANITIZE_STRING))
    {
    $week = $weekOfToday;
    }
        else
        {
        $week = filter_input(INPUT_GET, ‘wk’, FILTER_SANITIZE_STRING);
        }

I'd use this code:

if(filter_has_var(INPUT_GET, 'wk'))
  $week=filter_input(INPUT_GET, 'wk', FILTER_SANITIZE_STRING);
if(empty($week)) $week=$weekOfToday;

It looks like the problem with your code is really just the quotes around wk though. Use regular single quotes.

Something else which seems strange to me is that $week is being filtered as a string. Weeks are usually handled as numbers. As in the number of weeks since the year began. If that is the case then this is much better code:

if(filter_has_var(INPUT_GET, 'wk'))
  $week=filter_input(INPUT_GET, 'wk', FILTER_SANITIZE_NUMBER_INT);
if(empty($week)||$week<1||$week>53) $week=$weekOfToday;

Or something similar. The problem with dealing in week numbers is that there are a few different systems for determining the first week of the year. Sometimes it is the week of the first Sun, sometimes the first Mon but usually it is the week with the first Thur . You are going to need to figure out which system you are using. Because it is difficult to even explain this to a client I suggest making your program in such a way that when they change their mind about which one they want to use it is a simple fix on your end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM