简体   繁体   中英

Why am I getting undefined variable error here?

I have this code in a tpl.php file

<?php foreach ($images as $image): ?>
  <?php print $image ."\n"; ?>
<?php endforeach; ?>

I have the following in a preprocess function

function preprocess(&$vars) {
  // Initialize our $images array.
  $vars['images'] = array();

  foreach ($vars['rows'] as $item) {
    if (preg_match('@(<a.*?img.*?</a>)@i', $item, $matches)) {
      $image = $matches[1];
    }
    elseif (preg_match('@(<\s*img\s+[^>]*>)@i', $item, $matches)) {
      $image = $matches[1];
    }
    else {$images = NULL;}
    // Add the image to our image array
    $vars['images'][] = $image;
  }

Undefined variable: image at this line in the preprocess function

  $vars['images'][] = $image;

Typo.

$images = NULL;

should be

$image = NULL;

else :您有多个$ images而不是$ image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM