【问题标题】:PHP custom error issuePHP自定义错误问题
【发布时间】:2012-06-13 22:15:49
【问题描述】:

目前,我有这个代码:

<?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!";
}
?>

非常感谢对我的问题有任何帮助!

【问题讨论】:

  • 我想要一件蓝莓兔子服装
  • 与问题无关的杂乱代码太多。尝试归结/编译一个合适的测试用例。
  • 旁白:应该有[scripting] 标签的问题示例是什么?关于编写口译员的问题?当然,所有 ruby​​、python、php 等问题都不应该仅仅因为它是其中一种语言而用脚本自动标记。

标签: php scripting


【解决方案1】:
else { 
    echo "Item does not exist!"; 
} 

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

也许你把 else 块放错了地方?

【讨论】:

    【解决方案2】:

    看PHP的comparison operators

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 2011-01-11
      • 1970-01-01
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多