PHP:未定义的偏移量 [英] PHP: Undefined offset

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

问题描述

在某些页面上,我收到错误消息:

PHP注意:未定义偏移量:1 in /var/www/example.com/includes/head.php,第23行

这是代码:

if ($r)
{

    list($r1, $r2)=explode(" ", $r[0],2);
    $r1 = mb_strtolower($r1);
    $r3 = " ";
    $r2 = $r3.$r2;
    $r[0] = $r1.$r2;
    $page_title_f = $r[0]." some text";
    $page_title_s = "some text ";
    $page_title = $page_title_s.$page_title_f;

}

第23行出现错误:

list($r1, $r2)=explode(" ", $r[0],2);

您能帮助理解可能是什么问题吗?

更新

谢谢大家的帮助!我部分解决了这个问题.

$r是数据库中的行. 该脚本需要一个字符串并开始进行操作.将大写字母转换为小写字母.并且据我所知,字符串必须有一个空格,否则会出现错误"Undefined offset".因为脚本会尝试找到第一个空格,然后将第一个空格之前的单词与该空格以及一个空格之后出现的所有内容合并在一起. (:我不明白他为什么这么做,并且如果字符串中的空格为no,就不会出现这种情况,他只会抛出一个错误.):通常,这是一个非常老旧的引擎网上商店,称为Shop-脚本.发布文件的完整列表,也许会更清晰.

http://pastebin.com/Pz1TKpr3

解决方案

您收到的未定义索引错误是因为您要在变量($r)中爆炸的偏移量不存在.

您可以通过执行以下操作检查$r是什么:

print_r($r);

var_dump($r);

在进一步调试此问题之前,您需要显示$r的内容.

但是,您可能会想到,您的$r变量是您要爆炸的字符串,但是您试图将其作为数组访问.

如果这样爆炸会发生什么:

list($r1, $r2) = explode(' ', $r, 2);

On some pages, i receive the error:

PHP Notice: Undefined offset: 1 in /var/www/example.com/includes/head.php on line 23

Here is the code:

if ($r)
{

    list($r1, $r2)=explode(" ", $r[0],2);
    $r1 = mb_strtolower($r1);
    $r3 = " ";
    $r2 = $r3.$r2;
    $r[0] = $r1.$r2;
    $page_title_f = $r[0]." some text";
    $page_title_s = "some text ";
    $page_title = $page_title_s.$page_title_f;

}

Line 23 with error:

list($r1, $r2)=explode(" ", $r[0],2);

Could you help to understand what could be the problem?

Update

Thanks all for the help! I partially solved the problem.

$r is row in the database. The script takes a string and starts to manipulate. Converts uppercase letters to lowercase. And as I understand it, the string must have a space otherwise gets out an error "Undefined offset". Because the script tries to find the first space, and then merge the word before the first space, and the space together with everything that appears after a space. (: I don't understand why he does and there is no way out of this situation if the space in the string no, he just throws an error. ): In general it is a very old and poor engine web store called Shop-Script. Post a full listing of the file, maybe it will be clearer.

http://pastebin.com/Pz1TKpr3

解决方案

The undefined index error you are receiving is because the offset that you're trying to explode in the variable ($r) doesn't exist.

You can check what $r is by doing the following:

print_r($r);

or

var_dump($r);

You'll need to show what $r holds before debugging this issue further.

But a guess would be that your $r variable is a string that you're trying to explode, but you're trying to access it as an array.

What happens if you explode it like this:

list($r1, $r2) = explode(' ', $r, 2);

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

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