Php 在主IF外部打印数组值

Php 在主IF外部打印数组值,php,Php,我有一个问题,并试图解决了很长一段时间,但仍然没有运气。希望有人能帮助我 我试图验证一个表单,在我的验证过程中,我使用一个数组来存储错误消息。然后我需要在验证代码的外部打印该数组 这是我的验证码 // For storing registration errors: $reg_errors = array(); // Check for Write Testimonial form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') {

我有一个问题,并试图解决了很长一段时间,但仍然没有运气。希望有人能帮助我

我试图验证一个表单,在我的验证过程中,我使用一个数组来存储错误消息。然后我需要在验证代码的外部打印该数组

这是我的验证码

// For storing registration errors:
$reg_errors = array();

// Check for Write Testimonial form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // Check for Name:
    if (preg_match ('/^[A-Z \'.-]{2,80}$/i', $_POST['name'])) {
        $name = mysqli_real_escape_string ($dbc, $_POST['name']);
    } else {
        $reg_errors['name'] = 'Name : This field is required.';
    }

    // define a constant for the maximum upload size
    define ('MAX_FILE_SIZE', 1048576 );

    if (array_key_exists('testimonial_image', $_POST)) {

        // Create a temporary file name:
        $file = time() . '-' . $_FILES['testimonial_photo']['name'];

        // convert the maximum size to KB
        $max = number_format(MAX_FILE_SIZE/1024, 1).'KB';

        // create an array of permitted MIME types
        $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');

        // begin by assuming the file is unacceptable
        $sizeOK = false;
        $typeOK = false;

        // check that file is within the permitted size
        if ($_FILES['testimonial_photo']['size'] > 0 && $_FILES['testimonial_photo']['size'] <= MAX_FILE_SIZE) {
            $sizeOK = true;
        }

        // check that file is of an permitted MIME type
        foreach ($permitted as $type) {
            if ($type == $_FILES['testimonial_photo']['type']) {
                $typeOK = true;
                break;
            }
        }

        if ($sizeOK && $typeOK) {
            switch($_FILES['testimonial_photo']['error']) {
                case 0:

                // check if a file of the same name has been uploaded
                if (!file_exists(UPLOAD_DIR.'/'.$file)) {
                    // move the file to the upload folder and rename it
                    $success = move_uploaded_file($_FILES['testimonial_photo']['tmp_name'], UPLOAD_DIR.'/'.$file);
                } 

                if (!$success) {
                    $reg_errors[] = "Error uploading $file. Please try again.1";
                    echo "Error uploading $file. Please try again.1";
                }
                break;
                case 3:
                    $reg_errors[] = "Error uploading $file. Please try again.2";
                    echo "Error uploading $file. Please try again.2";
                    exit(); // Quit the script.                         

                default:                    
                    $reg_errors[] = "System error uploading $file. Contact webmaster.";                     
                    echo "System error uploading $file. Contact webmaster.";                        
            }

        } elseif ($_FILES['testimonial_photo']['error']) {      
                        $reg_errors[] = 'No file selected';                 
                        echo 'No file selected';                    
        } else {                
            $reg_errors[] = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: GIF, JPEG, PNG.";         
            echo  "$file cannot be uploaded. Maximum size: $max. Acceptable file types: GIF, JPEG, PNG.";           
        }

        echo $file;
    }

    // Delete the file if it still exists:
    if (file_exists ($_FILES['testimonial_photo']['tmp_name']) && is_file($_FILES['testimonial_photo']['tmp_name']) ) {
        unlink ($_FILES['testimonial_photo']['tmp_name']);
    }
    //print_r( $reg_errors);    
}

为什么要添加
全局$reg\u错误在第二个代码块的开头?我猜您从第一个代码块覆盖了
$reg\u错误。

从验证代码生成一个函数(返回$reg\u错误)。 在你想去的地方叫它。 不要使用全局变量


如果是同一个文件,请删除全局$reg_错误,它应该可以工作。

这两个脚本有何关联?它们在同一个文件中吗?分开文件?包括在内吗?您确定在尝试显示第二个脚本时,reg_错误不会被清空吗?这两个脚本位于同一文件和不同的位置。不包括在内。我确定数组不是空的。起初我尝试不使用它。但它不起作用。然后我试着用它
<div class="group">

    <!-- Display Error Messages -->
    <?php
        global $reg_errors; 
        print_r( $reg_errors); 

        if(!empty($reg_errors)) {
            echo '<div class="error">
                        <img src="images/error.png" />
                        <h3>Errors found in this form</h3>
                        <h4>Whoops! - There is a problem with the form, please check and correct the following.</h4>
                        <ul>';
                foreach( $reg_errors AS $error ) {
                    echo "<li>$error</li>";
                                            }

            echo '   </ul>
                    </div>';                                    
        }
?>  
</div>