PHP自定义错误问题

时间:2012-06-10 22:24:38

标签: php scripting

目前,我有这段代码:

<?php
if (isset($_GET['s'])) {
    $itemid = $_GET['s'];
    $search = "$itemid";
    $query  = ucwords($search);
    echo "<title>Results for $query</title>";
    $string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
    if ($itemid == "") {
        echo "Please fill out the form.";
    } else {
        echo '<div id="content">';
        $string = explode('<br>', $string);
        foreach ($string as $row) {
            preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
            if (preg_match("/$query/i", "$matches[1]")) {
                echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
                echo $matches[1];
                echo "</a><br>";
            }
        }
        echo '</div>';
    }
} else {
    echo "Item does not exist!";
}
?>

如果“$ matches [1]”中没有任何内容,我希望我的代码回显“项目不存在!”我怎么做呢?请帮忙!

我之前尝试过类似if ($matches[1]=="") { echo "Item does not exist!"; }的内容,但它没有用。这就是我得到的:

http://img685.imageshack.us/img685/998/28990b2c12d0423292d3574.png

看到工作正常吗?看看如果$ matches [1]存在会发生什么:

http://img528.imageshack.us/img528/3690/71472c9de6ec49118ee8d48.png

它仍然出现!如果$ matches [1]没有任何内容,我怎样才能使我的代码只能回复错误?请帮助我!

如果您想知道,在我添加if ($matches[1]=="") { echo "Item does not exist!"; }时,这是我的代码:

<?php
if (isset($_GET['s'])) {
    $itemid = $_GET['s'];
    $search = "$itemid";
    $query  = ucwords($search);
    echo "<title>Results for $query</title>";
    $string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
    if ($itemid == "") {
        echo "Please fill out the form.";
    } else {
        echo '<div id="content">';
        $string = explode('<br>', $string);
        foreach ($string as $row) {
            preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
            if (preg_match("/$query/i", "$matches[1]")) {
                echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
                echo $matches[1];
                echo "</a><br>";
            }
        }
        echo '</div>';
        if ($matches[1] == "") {
        echo "Item does not exist!";
        }
    }
} else {
    echo "Item does not exist!";
}
?>

对我的问题的任何帮助都会非常高兴!

2 个答案:

答案 0 :(得分:0)

看看PHP的comparison operators

if (empty($matches[1])) { echo "Item does not exist!"; }

答案 1 :(得分:0)

else { 
    echo "Item does not exist!"; 
} 

这个“其他”与isset($_GET['s'])有关。查询字符串中是否有一个名为s的变量?如果没有,那么你收到消息是正常的。

也许你把那个else块放在了错误的地方?