duanmiyang6201 2015-11-09 08:00
浏览 17
已采纳

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?

  • 写回答

1条回答 默认 最新

  • dongyan3853 2015-11-09 08:15
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作