在 isset() 中添加函数而不是 运行 PHP 脚本
Adding Function in isset() not Running PHP Script
我有一个表单页面,用户可以在其中填写他们的信息。当用户提交他们的信息时,函数 userName
应该 return 他们的信息在欢迎字符串 $messageNew
中。我已经向 isset()
添加了一个函数,如果不为空,该函数应该 return 值。
当我从脚本中删除函数 'userName()' 时,脚本运行正常并显示 HTML 正文。当我在脚本中添加函数时,页面变为空白。
我正在尝试练习使用 PHP 到 return 值中的函数。我知道函数中缺少某些东西(syntax/logic?)但我不知道它是什么。如有任何意见让我知道我的脚本中缺少什么,我们将不胜感激。
这里是 PHP(连同 HTML)
<?php
if(isset($_POST['submit'])) {
$first = $_POST['firstName'];
$number = $_POST['numberValue'];
$sentence = $_POST['sentenceValue'];
$welcome = "Welcome to this site!";
$show_form = false;
function userName($first) {
$messageNew = echo '$first . $welcome';
if(!empty($first)) {
return $messageNew;
}
else {
return false;
}
} // end function userName
}
else {
$show_form = true;
$error_message = "";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Lesson6</title>
</head>
<body>
<?php
if($show_form) {
?>
<form name="userForm" method="post" action="<?php echo $_SERVER[ 'PHP_SELF' ]?>">
<table>
<tr>
<td>First Name:</td><td><input type="text" name="firstName" id="firstName" value="<?php echo $first ?>" /></td>
</tr>
<tr>
<td>Enter a number between 100 & 200:</td><td><input type="text" name="numberValue" id="numberValue" value="<?php echo $number ?>" /></td>
</tr>
<tr>
<td>Enter a sentence of at least 20 characters:</td><td><input type="text" name="sentenceValue" id="sentenceValue" value="<?php echo $sentence ?>" /></td>
</tr>
<tr>
<td>
<p><?php echo $error_message ?></p>
<p><?php echo $messageNew ?></p>
</td>
</tr>
<tr>
<td><input type="submit" name="submit" id="submit" /></td>
</tr>
</table>
</form>
<?php
} else {
?>
<p><?php echo $welcome; ?></p>
<?php
}
?>
</body>
你的页面是空的,因为你有一个错误并且没有打开错误报告。所以从这样做开始吧。
(警告,您的代码是废话)
您的错误是您在变量赋值中使用了 echo
。你不能也不需要这样做。只需将字符串分配给变量即可。
此外,您正在尝试使用 $welcome
,即使由于范围的原因它对您的函数不可用。您需要像 $first
一样将它作为参数传递给 userName()
(我不知道您为什么做一个而不做另一个)。
此外,您尝试使用这两个值 ,然后 检查它们是否有效。那是向后。
此外,单引号内有变量。它们不是插值在那里。使用双引号。
更进一步,您不在字符串内部使用连接运算符。您可以使用它们来连接字符串。
function userName($first, $welcome) {
if(!empty($first)) {
$messageNew = "$first $welcome";
return $messageNew;
}
else {
return false;
}
}
您也可以通过直接返回字符串来缩短它:
function userName($first, $welcome) {
if(!empty($first)) {
return "$first $welcome";
}
else {
return false;
}
}
我有一个表单页面,用户可以在其中填写他们的信息。当用户提交他们的信息时,函数 userName
应该 return 他们的信息在欢迎字符串 $messageNew
中。我已经向 isset()
添加了一个函数,如果不为空,该函数应该 return 值。
当我从脚本中删除函数 'userName()' 时,脚本运行正常并显示 HTML 正文。当我在脚本中添加函数时,页面变为空白。
我正在尝试练习使用 PHP 到 return 值中的函数。我知道函数中缺少某些东西(syntax/logic?)但我不知道它是什么。如有任何意见让我知道我的脚本中缺少什么,我们将不胜感激。
这里是 PHP(连同 HTML)
<?php
if(isset($_POST['submit'])) {
$first = $_POST['firstName'];
$number = $_POST['numberValue'];
$sentence = $_POST['sentenceValue'];
$welcome = "Welcome to this site!";
$show_form = false;
function userName($first) {
$messageNew = echo '$first . $welcome';
if(!empty($first)) {
return $messageNew;
}
else {
return false;
}
} // end function userName
}
else {
$show_form = true;
$error_message = "";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Lesson6</title>
</head>
<body>
<?php
if($show_form) {
?>
<form name="userForm" method="post" action="<?php echo $_SERVER[ 'PHP_SELF' ]?>">
<table>
<tr>
<td>First Name:</td><td><input type="text" name="firstName" id="firstName" value="<?php echo $first ?>" /></td>
</tr>
<tr>
<td>Enter a number between 100 & 200:</td><td><input type="text" name="numberValue" id="numberValue" value="<?php echo $number ?>" /></td>
</tr>
<tr>
<td>Enter a sentence of at least 20 characters:</td><td><input type="text" name="sentenceValue" id="sentenceValue" value="<?php echo $sentence ?>" /></td>
</tr>
<tr>
<td>
<p><?php echo $error_message ?></p>
<p><?php echo $messageNew ?></p>
</td>
</tr>
<tr>
<td><input type="submit" name="submit" id="submit" /></td>
</tr>
</table>
</form>
<?php
} else {
?>
<p><?php echo $welcome; ?></p>
<?php
}
?>
</body>
你的页面是空的,因为你有一个错误并且没有打开错误报告。所以从这样做开始吧。
(警告,您的代码是废话)
您的错误是您在变量赋值中使用了 echo
。你不能也不需要这样做。只需将字符串分配给变量即可。
此外,您正在尝试使用 $welcome
,即使由于范围的原因它对您的函数不可用。您需要像 $first
一样将它作为参数传递给 userName()
(我不知道您为什么做一个而不做另一个)。
此外,您尝试使用这两个值 ,然后 检查它们是否有效。那是向后。
此外,单引号内有变量。它们不是插值在那里。使用双引号。
更进一步,您不在字符串内部使用连接运算符。您可以使用它们来连接字符串。
function userName($first, $welcome) {
if(!empty($first)) {
$messageNew = "$first $welcome";
return $messageNew;
}
else {
return false;
}
}
您也可以通过直接返回字符串来缩短它:
function userName($first, $welcome) {
if(!empty($first)) {
return "$first $welcome";
}
else {
return false;
}
}