未定义的偏移PHP错误 [英] undefined offset PHP error

查看:110
本文介绍了未定义的偏移PHP错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中收到以下错误

I am receiving the following error in PHP

注意未定义的偏移量1:位于C:\ wamp \ www \ includes \ imdbgrabber.php第36行

Notice undefined offset 1: in C:\wamp\www\includes\imdbgrabber.php line 36

这是导致它的PHP代码:

Here is the PHP code that causes it:

<?php

# ...

function get_match($regex, $content)  
{  
    preg_match($regex,$content,$matches);     

    return $matches[1]; // ERROR HAPPENS HERE
}

错误是什么意思?

推荐答案

如果 preg_match 未找到匹配,$matches是一个空数组.因此,在访问$matches[0]之前,您应该检查preg_match是否找到匹配项,例如:

If preg_match did not find a match, $matches is an empty array. So you should check if preg_match found an match before accessing $matches[0], for example:

function get_match($regex,$content)
{
    if (preg_match($regex,$content,$matches)) {
        return $matches[0];
    } else {
        return null;
    }
}

这篇关于未定义的偏移PHP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆