Php 基于$\u GET编辑HTML
您试图在函数内部使用函数外部的变量,而函数的工作方式与您想象的不一样 请仔细阅读变量范围: 如果您将功能更改为:Php 基于$\u GET编辑HTML,php,Php,您试图在函数内部使用函数外部的变量,而函数的工作方式与您想象的不一样 请仔细阅读变量范围: 如果您将功能更改为: 在顶部,这将按您希望的方式工作,但它是错误的。$t和其他不在全局范围内。归还它们 <?php $g = $_GET['e']; $t = "Title!"; $h = ""; $p = ""; function errorput($et,$eh,$ep) { $t = $et; $h = '<h1>'.$eh.'</h1>';
在顶部,这将按您希望的方式工作,但它是错误的。$t和其他不在全局范围内。归还它们
<?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>
谢谢你的链接;我只需要在函数中放入:global$t、$h、$p
。如果可以避免,在函数中不使用global可能是更好的做法。创世记建议返回一个数组。
<?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>