将变量作为“条件”传递给函数(imap_sort)

时间:2014-02-06 16:58:19

标签: php

试图找到一种方法将变量作为标准传递

$_GET['sort'] = (@!$_GET['sort']) ? SORTARRIVAL : SORT . $_GET['sort'];
$sort = imap_sort ($inbox, $_GET['sort'] , 1, SE_UID);

如果我传递了诸如... /?sort = SUBJECT

之类的网址

我收到错误。

  

警告:imap_sort()期望参数2为long,字符串为

1 个答案:

答案 0 :(得分:0)

由于你的$ _GET值是一个包含你想要使用的常量的 name 的字符串,而imap期望常量的,你需要转一个到另一个(即你说“SORTARRIVAL”,而imap期望SORTARRIVAL的(类似于1或2或3或其他)。

您可以使用constant()函数查找常量,如下所示:

if(isset($_GET['sort']) && constant('SORT' . $_GET['sort']) !== null) {
   // we check if we have a value in $_GET['sort'] and also check if a constant with that name exists
   $sort = constant('SORT' . $_GET['sort'])
} else {
   // default value in case the constant didn't exist or $_GET['sort'] was empty
   $sort = SORTARRIVAL;
}
$sort = imap_sort ($inbox, $sort, 1, SE_UID);