Search code examples
phpphp-gettext

Syntax error in gettext.php library saying "unexpected '!=' (T_IS_NOT_EQUAL)"


I am using the library gettext.php (not the standard php_gettext extension) and the error

PHP Parse error: syntax error, unexpected '!=' (T_IS_NOT_EQUAL) in /base/data/home/apps/.../libs/gettext/gettext.php(387) : eval()'d code on line 1 PHP Notice: Undefined offset: -1 in /base/data/home/apps/.../libs/gettext/gettext.php on line 422

keeps appearing on the following line:

$taskCount = Group::activeTaskCount($db, $class[Database::FIELD_CLASS_ID]);
echo ngettext(
        '%d pending task',
        '%d pending tasks',
        $taskCount);

The function Group::activeTaskCount() performs an SQL query and returns a COUNT(*) of a query.


Solution

  • I found that $taskCount was actually not returning an integer value, but rather NULL. And the gettext.php library could not handle this and threw this error.

    I changed the code to

    echo ngettext(
            '%d pending task',
            '%d pending tasks',
            $taskCount ?: 0);
    

    so as to foresee the NULL case.