简体   繁体   English

为什么我在这里得到未定义的变量错误?

[英]Why am I getting undefined variable error here?

I have this code in a tpl.php file 我在tpl.php文件中有此代码

<?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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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