PHP Deprecated: strcmp(): in SearchBuilderOptions.php

PHP Deprecated: strcmp(): in SearchBuilderOptions.php

Stacey1134Stacey1134 Posts: 112Questions: 20Answers: 0

My first Discussion and not a question. My new production environment is using PHP 8.3 so I recently modified my local environments to do the same. I immediately got this error and Datatables refused to load:

[Fri Jun 21 12:41:05.044870 2024] [php:notice] [pid 24716:tid 1896] [client 127.0.0.1:57495] PHP Deprecated:  strcmp(): Passing null to parameter #2 ($string2) of type string is deprecated in D:\\Websites\\site4.local\\assets\\dataTables\\lib\\Editor\\SearchBuilderOptions.php on line 328, referer: http://site4.local:8080/databases/members.php

As I try to steer away from updating dataTables files directly, I re-downloaded Editor 2.3.2, but got the same error.
Here is the changes I made, Line 328 was causing the error. Hope this helps anyone else.

Old lines in SearchBuilderOptions.php

        // Only sort if there was no SQL order field
        if (!$this->_order) {
            usort($out, static function ($a, $b) {
                return is_numeric($a['label']) && is_numeric($b['label']) ?
                    ($a['label'] * 1) - ($b['label'] * 1) :
                    strcmp($a['label'], $b['label']);
            });
        }

               return $out;

New lines in SearchBuilderOptions.php

        // Only sort if there was no SQL order field
        // if (!$this->_order) {
        //  usort($out, static function ($a, $b) {
        //      return is_numeric($a['label']) && is_numeric($b['label']) ?
        //          ($a['label'] * 1) - ($b['label'] * 1) :
        //          strcmp($a['label'], $b['label']);
        //  });
        // }

        // return $out;

        // Only sort if there was no SQL order field
        if (!$this->_order) {
            usort($out, static function ($a, $b) {
                // Check if both labels are numeric
                if (is_numeric($a['label']) && is_numeric($b['label'])) {
                    // Numeric comparison
                    return ($a['label'] * 1) - ($b['label'] * 1);
                } else {
                    // String comparison, ensuring no null values are passed to strcmp
                    return strcmp((string)($a['label'] ?? ''), (string)($b['label'] ?? ''));
                }
            });
        }

        return $out;

Replies

  • allanallan Posts: 63,488Questions: 1Answers: 10,468 Site admin

    Thank you! Fix committed here. Looks like I had fixed it for SearchPanes already, but missed SearchBuilder (and the Options class). Apologies for that. It will be included in the next release of Editor.

    Allan

Sign In or Register to comment.