PHP包含未声明的变量

with PHP I'm trying to check if the input fields are valid or not, while doing this I'm putting my codes into different php files and include them in the main php file(registerFunction.php), the reason I'm doing this is to keep things organised that will run on the click of a button but I get an error that the variable I declared in the registerFunction.php is undeclared in the regValidation.php file. Following is the error message I get: Undefined variable: regErrors in

my Code for registerFunction.php is:

<?php 

//Create Error array
$regErrors = array();
//Add Date Functions
include "includes/functions/dateFunction.php";

//Checks if Register Screen Inputs are valid or not and pushes the errors into an array.
include "includes/functions/checkRegFields.php";


//
include "includes/functions/registryValidation.php";


if(regDecision()){
    //register
}
else{
    foreach ($regErrors as $errrorMessage) {
        ?>
            <script>
                $("#errors").append('<div class="alert alert-danger" role="alert"><?php echo $errrorMessage; ?></div>');
            </script>
        <?php
        }
}
    echo postDate();

?>

checkRegFields.php :

<?php


if(isset($_POST['registerButton'])){

if(($_POST['regEmail'])){
    $regEmail = $_POST['regEmail'];
}
else{
    array_push($regError,"Please Fill in the Email field");
}

if(($_POST['regUsername'])){
    $regUsername = $_POST['regUsername'];
}
else{

    array_push($regErrors,"Please Fill in the Username field");
}


if ((['regPassword']) and ($_POST['regPassword2'])) {


    if( ($_POST['regPassword']) == ($_POST['regPassword2']) ){
        $regPassword = $_POST['regPassword'];
    }
    else{
        array_push($regErrors,"Passwords does not match!");
    }
}
else{
    array_push($regErrors,"Please Fill in the Password fields");
}
}   

?>

and Finally I have registryValidation.php(where I'm having an error):

<?php 

function regDecision(){

if(sizeof($regErrors)==0){

    return true;
}
else{
    return false;
}
}

?>

checkRegFields.php is fine with the $regError variable but registryValidation.php tells me that $regError is undeclared.

why is this?

By default, when you use a variable in a function, it is scoped to that function only. If you have a global (free) variable that you want to make available in the function, you have to make it global:

<?php 

function regDecision() { 
  // Use the globally defined version of the variable, use:
  global $regErrors;

  return sizeof($regErrors) == 0;
}

?>

Note how I also replaced your long-winded if(condition) return true; else return false; with a shorter return condition.

In addition, I saw that you have misspelled the variable name in the if-statement that validates your email:

array_push($regError,"Please Fill in the Email field");

should be

array_push($regErrors,"Please Fill in the Email field");

You can avoid such errors by enabling strict reporting, which will warn you about using undefined variables by adding

error_reporting(E_ALL | E_STRICT);

to the script, or globally in php.ini:

error_reporting = E_ALL | E_STRICT