PHP-致命错误:调用未定义函数
我是PHP新手,试图找出在调用html中的checkFirst()和checkPass()时它抛出此错误的原因,代码如下-我将**放在了问题产生的地方: 错误:致命错误:调用第12行C:\wamp\www\form\index.php中未定义的函数checkFirst()PHP-致命错误:调用未定义函数,php,Php,我是PHP新手,试图找出在调用html中的checkFirst()和checkPass()时它抛出此错误的原因,代码如下-我将**放在了问题产生的地方: 错误:致命错误:调用第12行C:\wamp\www\form\index.php中未定义的函数checkFirst() <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Form&
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form action="index.php" method="post">
<div class="signup_wrap">
<div class="wrap"><label for="first">First Name:</label><input type="text" name="first" value=""></div>
**<?php checkFirst($first); ?>**
<div class="wrap"><label for="first">Last Name:</label> <input type="text" name="last" value=""></div>
<div class="wrap"><label for="first">Password:</label> <input type="text" name="first_password" value=""></div>
<div class="wrap"><label for="first">Confirm password:</label><input type="text" name="second_password" value=""></div>
**<?php checkPass($password1, $password2);?>**
<br>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
$password1 = "";
$first = "";
if (isset($_POST['submit'])) {
$first = $_POST['first'];
$password1 = $_POST['first_password'];
$password2 = $_POST['second_password'];
function checkFirst($first){
if (empty($first)) {
echo "Please enter text";
return false;
}
return true;
}
function checkPass($password1, $password2){
if (empty($password2)) {
echo "Please enter text";
return false;
}
if ($password1 == $password2) {
echo "Match";
return true;
} else {
echo "False";
return false;
}
return true;
}
function checkAll($first, $password1, $password2){
if (checkPass($password1, $password2) && checkFirst($first)) {
header("Location: http://www.trademe.co.nz");
return true;
} else {
echo "Nope";
return false;
}
}
checkAll($first, $password1, $password2);
}
?>
形式
名字:
****
姓氏:
密码:
确认密码:
****
您正在定义表单提交的if
条件中的函数
if (isset($_POST['submit'])) {
因此,只有在if
条件下,这些函数才具有可访问性。您可以将函数置于条件之外
if (isset($_POST['submit'])) { // start of condition
$first = $_POST['first'];
$password1 = $_POST['first_password'];
$password2 = $_POST['second_password'];
----------------------
} // end of condition
/* Common Functions */
function checkFirst($first){
if (empty($first)) {
echo "Please enter text";
return false;
}
return true;
}
<?php
$password1 = "";
$first = "";
if (isset($_POST['submit'])) {
$first = $_POST['first'];
$password1 = $_POST['first_password'];
$password2 = $_POST['second_password'];
checkAll($first, $password1, $password2);
}
function checkFirst($first){
if (empty($first)) {
echo "Please enter text";
return false;
}
return true;
}
function checkPass($password1, $password2){
if (empty($password2)) {
echo "Please enter text";
return false;
}
if ($password1 == $password2) {
echo "Match";
return true;
} else {
echo "False";
return false;
}
return true;
}
function checkAll($first, $password1, $password2){
if (checkPass($password1, $password2) && checkFirst($first)) {
header("Location: http://www.trademe.co.nz");
return true;
} else {
echo "Nope";
return false;
}
}
?>
函数应该在if之前(isset($\u POST['submit'])){
形式
名字:
****
姓氏:
密码:
确认密码:
****
您的PHP代码必须如下所示。函数应该在if条件之外定义
if (isset($_POST['submit'])) { // start of condition
$first = $_POST['first'];
$password1 = $_POST['first_password'];
$password2 = $_POST['second_password'];
----------------------
} // end of condition
/* Common Functions */
function checkFirst($first){
if (empty($first)) {
echo "Please enter text";
return false;
}
return true;
}
<?php
$password1 = "";
$first = "";
if (isset($_POST['submit'])) {
$first = $_POST['first'];
$password1 = $_POST['first_password'];
$password2 = $_POST['second_password'];
checkAll($first, $password1, $password2);
}
function checkFirst($first){
if (empty($first)) {
echo "Please enter text";
return false;
}
return true;
}
function checkPass($password1, $password2){
if (empty($password2)) {
echo "Please enter text";
return false;
}
if ($password1 == $password2) {
echo "Match";
return true;
} else {
echo "False";
return false;
}
return true;
}
function checkAll($first, $password1, $password2){
if (checkPass($password1, $password2) && checkFirst($first)) {
header("Location: http://www.trademe.co.nz");
return true;
} else {
echo "Nope";
return false;
}
}
?>
像这样更新最后一个函数
function checkAll($first, $password1, $password2){
$chekPass = checkPass($password1, $password2);
$chekFirst = checkFirst($first);
if ( $checkPass && $checkFirst ) {
header("Location: http://www.trademe.co.nz");
return true;
} else {
echo "Nope";
return false;
}
}
由于您在
if
块中定义函数,因此它们只有在测试该条件后才被定义,这是真的。您试图在if
测试之前调用函数,因此函数尚未定义
通常,您应该在脚本的顶层定义函数,然后它们可以从脚本中的任何位置使用,因为它们不依赖于某个条件。函数应该在if条件之外定义,否则,除非您处于该条件,否则您无法访问这些函数。第12行是t说“此外,函数只有在定义后才可访问-即使在定义之前尝试在
if
块内部访问也是一个问题。谢谢,非常感谢:)@Jenz回答得很好…)谢谢,非常感谢:)谢谢,非常感谢:)
if (isset($_POST['submit'])) { // start of condition
$first = $_POST['first'];
$password1 = $_POST['first_password'];
$password2 = $_POST['second_password'];
----------------------
} // end of condition
/* Common Functions */
function checkFirst($first){
if (empty($first)) {
echo "Please enter text";
return false;
}
return true;
}
<?php
$password1 = "";
$first = "";
if (isset($_POST['submit'])) {
$first = $_POST['first'];
$password1 = $_POST['first_password'];
$password2 = $_POST['second_password'];
checkAll($first, $password1, $password2);
}
function checkFirst($first){
if (empty($first)) {
echo "Please enter text";
return false;
}
return true;
}
function checkPass($password1, $password2){
if (empty($password2)) {
echo "Please enter text";
return false;
}
if ($password1 == $password2) {
echo "Match";
return true;
} else {
echo "False";
return false;
}
return true;
}
function checkAll($first, $password1, $password2){
if (checkPass($password1, $password2) && checkFirst($first)) {
header("Location: http://www.trademe.co.nz");
return true;
} else {
echo "Nope";
return false;
}
}
?>