基于$ _GET编辑HTML

时间:2011-10-27 18:13:38

标签: php

<?php
$g = $_GET['e'];
$t = "Title!";
$h = "";
$p = "";

function errorput($et,$eh,$ep) {
    $t = $et;
    $h = '<h1>'.$eh.'</h1>';
    $p = '<p>'.$ep.'</p>';
}

if ($g == "nodata") {
    errorput("Missing Something...", "Blank Field", "You left a box or few empty.");
} elseif ($g == "nopass") {
    errorput("Password Incorrect!", "Encrypted Hash Unmatched", "Your password is probably wrong.");
} else {
    errorput($t, "I have no idea.", "There was an error, but we don't know why.");
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $t ?></title>
<head>
<body>
<?php echo $h; echo $p; ?>
</body>
</html>

因此,它根据通过GET收到的内容输出html。 为什么不起作用?

2 个答案:

答案 0 :(得分:2)

$ t和其他人不在全球范围内。归还他们。

<?php
$g = $_GET['e'];
$t = "Title!";
$h = "";
$p = "";

function errorput($et, $eh, $ep)
{
    $t = $et;
    $h = '<h1>' . $eh . '</h1>';
    $p = '<p>' . $ep . '</p>';
    return array(
        $t,
        $h,
        $p
    );
}

if ($g == "nodata") {
    $errors = errorput("Missing Something...", "Blank Field", "You left a box or few empty.");
} elseif ($g == "nopass") {
    $errors = errorput("Password Incorrect!", "Encrypted Hash Unmatched", "Your password is probably wrong.");
} else {
    $errors = errorput($t, "I have no idea.", "There was an error, but we don't know why.");
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php
echo $errors[0];
?></title>
<head>
<body>
<?php
echo $errors[1] . $errors[2];
?>
</body>
</html>

答案 1 :(得分:0)

您正在尝试使用函数内部函数外部的变量,这些变量无法像您认为的那样工作。

请阅读变量范围:http://php.net/manual/en/language.variables.scope.php

如果您将功能更改为:     

在顶部,这将按照你想要的方式工作,但这是错误的。