Zen Cart Logo
Forums / Bug Reports / "Undefined variable" warning in zen_build_keyword_where_clause function

"Undefined variable" warning in zen_build_keyword_where_clause function

Views: 947

Results 1 to 2 of 2
5 Jan 2024, 4:19 PM
#1
yesaul avatar

yesaul

New Zenner

Join Date:
Apr 2011
Location:
Espoo, Finland
Posts:
71
Plugin Contributions:
0

"Undefined variable" warning in zen_build_keyword_where_clause function

Happy New Year everyone

PHP 8 can generate "Undefined variable" and "Passing null to parameter" warnings in zen_build_keyword_where_clause function (functions_strings.php, line 211)

    function zen_build_keyword_where_clause($fields, $string, $startWithWhere = false)
   {
        global $db, $zco_notifier;

        $zco_notifier->notify('NOTIFY_BUILD_KEYWORD_SEARCH', '', $fields, $string);
        if (zen_parse_search_string(stripslashes($string), $search_keywords)) [B]{[/B]
            [B]$where_str =[/B] " AND (";
 
            ...

        [B]}[/B]
        if (substr([B]$where_str[/B], -7) === '( ()  )') {
            return ' ';
        }
        return $where_str ?? ' ';
    }

My quick suggestion:

if (!empty($where_str) AND substr($where_str, -7) === '( ()  )') {
            return ' ';
        }
5 Jan 2024, 5:46 PM
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: "Undefined variable" warning in zen_build_keyword_where_clause function

There's a number of ways to potentially address:

  1. Pre-assign a default value, then change the final torn to just send the value of $where_str.
  2. Add a test for emptiness or lack of setting before checking if made of empty parentheses. The return of this test to be whatever default or representation of being empty. Then remove the test against being set to just return the $where_str value.
  3. Put the test of the empty parentheses inside the if statement that appears to generate that result. And apply variations of the above.

But, the solution adopted is/was in this pull request: https://github.com/zencart/zencart/pull/5747/files