Title: PHP Warning: Undefined variable $item
Last modified: August 29, 2024

---

# PHP Warning: Undefined variable $item

 *  Resolved [Kudratullah](https://wordpress.org/support/users/mhamudul_hk/)
 * (@mhamudul_hk)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/php-warning-undefined-variable-item/)
 * Hi,
   There’s an undefined variable `$item` in the beginning of `[NginCache::validate_dirlist( $list )](https://plugins.trac.wordpress.org/browser/nginx-cache/tags/1.0.6/nginx-cache.php#L210)`
   function.
 *     ```wp-block-code
       PHP Warning:  Undefined variable $item in /path-to-wp/wp-content/plugins/nginx-cache/nginx-cache.php on line 211PHP Warning:  Trying to access array offset on value of type null in /path-to-wp/wp-content/plugins/nginx-cache/nginx-cache.php on line 211
       ```
   
 * Thanks 🙂
    -  This topic was modified 1 year, 9 months ago by [Kudratullah](https://wordpress.org/support/users/mhamudul_hk/).

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [Roger Holman](https://wordpress.org/support/users/flight750/)
 * (@flight750)
 * [1 year, 9 months ago](https://wordpress.org/support/topic/php-warning-undefined-variable-item/#post-17993168)
 * I see this too – also, there’s a fix that Till Krüss merged today, so guessing
   we’ll see an update shortly!
 *  [Marcos Nobre](https://wordpress.org/support/users/onyx808/)
 * (@onyx808)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/php-warning-undefined-variable-item/#post-18023585)
 * The warnings you’re seeing are due to the fact that the variable `$item` is not
   defined properly before it is accessed in the `validate_dirlist` method of your
   plugin. Specifically, the code is trying to access the array offset on a variable
   that may not have been initialized.
 * The issue lies in this part of the code in the `validate_dirlist` method (around
   line 211):
 *     ```wp-block-code
       if ( $item['type'] === 'f' && strpos( $item, "." ) !== false ) {
           return true;
       }
       ```
   
 * The variable `$item` is being used without being initialized first. To resolve
   this, you’ll need to iterate through the `$list` array properly and ensure that`
   $item` is defined before trying to access its properties.
 * Here’s the corrected version of your `validate_dirlist` function:
 *     ```wp-block-code
       private function validate_dirlist( $list ) {
   
           foreach ( $list as $item ) {
   
               // Ensure $item is defined and is an array
               if ( ! isset( $item['type'] ) || ! is_array( $item ) ) {
                   continue; // Skip invalid or undefined items
               }
   
               // check if it's a file and contains a dot (.)
               if ( $item['type'] === 'f' && strpos( $item['name'], "." ) !== false ) {
                   return true;
               }
   
               // validate subdirectories recursively
               if ( $item['type'] === 'd' && ! empty( $item['files'] ) && is_array( $item['files'] ) ) {
                   if ( ! $this->validate_dirlist( $item['files'] ) ) {
                       return false;
                   }
               }
   
           }
   
           return true;
       }
       ```
   
 *  [Meagan](https://wordpress.org/support/users/mtruglio/)
 * (@mtruglio)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/php-warning-undefined-variable-item/#post-18048211)
 * Was this ever get patched? I’m still having this error occur quite often and 
   i have the most recent version of the plugin. The error prevents pages from loading
   and the browser times out. Hopefully Till Krüss can crush this bug.
   [error] 374080#
   374080: *5 FastCGI sent in stderr: “PHP message: PHP Warning: Undefined variable
   $item in /var/www/html/live/wp-content/plugins/nginx-cache/nginx-cache.php on
   line 211PHP message: PHP Warning: Trying to access array offset on value of type
   null in /var/www/html/live/wp-content/plugins/nginx-cache/nginx-cache.php on 
   line 211PHP message: PHP Warning: Undefined variable $item in /var/www/html/live/
   wp-content/plugins/nginx-cache/nginx-cache.php on line 211PHP message: PHP Warning:
   Trying to access array offset on value of type null in /var/www/html/live/wp-
   content/plugins/nginx-cache/nginx-cache.php on line 211” while reading response
   header from upstream, request: “GET /wp-admin/tools.php?page=nginx-cache&message
   =cache-purged HTTP/2.0”, upstream: “fastcgi://unix:/run/php/php8.1-fpm.sock:”
 *  Thread Starter [Kudratullah](https://wordpress.org/support/users/mhamudul_hk/)
 * (@mhamudul_hk)
 * [1 year, 7 months ago](https://wordpress.org/support/topic/php-warning-undefined-variable-item/#post-18056320)
 * Seems the bug is patched (in [GitHub](https://github.com/tillkruss/Nginx-FastCGI-Cache/blob/97dc4d3581f575402a2a90da514bbf1c247d2404/nginx-cache.php#L208-L230)),
   but the [svn release](https://plugins.trac.wordpress.org/browser/nginx-cache/tags/1.0.6/nginx-cache.php#L208)
   was not have the patch.
 * GitHub Version: [https://github.com/tillkruss/Nginx-FastCGI-Cache/blob/97dc4d3581f575402a2a90da514bbf1c247d2404/nginx-cache.php#L208-L230](https://github.com/tillkruss/Nginx-FastCGI-Cache/blob/97dc4d3581f575402a2a90da514bbf1c247d2404/nginx-cache.php#L208-L230)
 * SVN Version: [https://plugins.trac.wordpress.org/browser/nginx-cache/tags/1.0.6/nginx-cache.php#L208](https://plugins.trac.wordpress.org/browser/nginx-cache/tags/1.0.6/nginx-cache.php#L208)
 *  Plugin Author [Till Krüss](https://wordpress.org/support/users/tillkruess/)
 * (@tillkruess)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/php-warning-undefined-variable-item/#post-18139098)
 * Yep, fixed on GitHub. Will push a release next week.
 *  [Anax](https://wordpress.org/support/users/pournaras/)
 * (@pournaras)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/php-warning-undefined-variable-item/#post-18161991)
 * The proposed change doesn’t work. Specifically, at line 212:
 *     ```wp-block-code
               if ( $item[ 'type' ] === 'f' && strpos( $item, "." ) !== false ) {
                   return true;
               }
       ```
   
 * Given that variable $item is an array,
 *     ```wp-block-code
       strpos( $item, "." )
       ```
   
 * produces an error.
 * I think that line should read:
 *     ```wp-block-code
               if ( $item[ 'type' ] === 'f' && strpos( $item[ 'name' ], "." ) !== false ) {            return true;        }
       ```
   
 *  Plugin Author [Till Krüss](https://wordpress.org/support/users/tillkruess/)
 * (@tillkruess)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/php-warning-undefined-variable-item/#post-18162774)
 * Yep: [https://github.com/tillkruss/Nginx-FastCGI-Cache/pull/27/files](https://github.com/tillkruss/Nginx-FastCGI-Cache/pull/27/files)

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘PHP Warning: Undefined variable $item’ is closed to new replies.

 * ![](https://ps.w.org/nginx-cache/assets/icon-256x256.jpg?rev=1029092)
 * [Nginx Cache](https://wordpress.org/plugins/nginx-cache/)
 * [Support Threads](https://wordpress.org/support/plugin/nginx-cache/)
 * [Active Topics](https://wordpress.org/support/plugin/nginx-cache/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/nginx-cache/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/nginx-cache/reviews/)

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * 7 replies
 * 7 participants
 * Last reply from: [Till Krüss](https://wordpress.org/support/users/tillkruess/)
 * Last activity: [1 year, 6 months ago](https://wordpress.org/support/topic/php-warning-undefined-variable-item/#post-18162774)
 * Status: resolved