简体   繁体   English

从PHP中包含的函数文件向数组添加值

[英]Adding values to an array from an included functions file in PHP

I am new to programming PHP and I need help with what is i'm sure a simple question. 我是PHP编程的新手,我肯定需要帮助一个简单的问题。 I am trying to add values to an array called errors in my form page that way I can echo it out later for verification, although I cant seem to add anything to the array from my included functions file. 我试图在表单页面中将值添加到称为错误的数组中,这样我以后就可以对其进行回显以进行验证,尽管我似乎无法从包含的函数文件中向数组中添加任何内容。

I require functions <?php require_once("functions.php") ?> 我需要函数<?php require_once("functions.php") ?>

Then I create the array <?php $errors = array(); ?> 然后创建数组<?php $errors = array(); ?> <?php $errors = array(); ?>

Then I call the function from includes <?php minLength($test, 20); ?> 然后,我从include <?php minLength($test, 20); ?>调用该函数<?php minLength($test, 20); ?> <?php minLength($test, 20); ?>

function here 在这里起作用

function minLength($input, $min) { if (strlen($input) <= $min) { return $errors[] = "Is that your real name? No its not."; } else { return $errors[] = ""; } }

then echo them out at and like this at the end 然后在最后回声他们,像这样

<?php 
        if (isset($errors)) {
            foreach($errors as $error) {
    echo "<li>{$error}</li><br />";
        } 
        } else {
            echo "<p>No errors found </p>";
        }
        ?>

But nothing echos in the end, Thank you in advance for any help 但最后没有回音,在此先感谢您的帮助

minLength() function returns $errors as you defined. minLength()函数返回您定义的$errors However, you don't have $errors accepting return from that function in your code. 但是,您的代码中没有$errors接受该函数的返回。

Example code will be: 示例代码为:

<?php
    require_once("functions.php");
    $errors = array();

    $errors = minLength($test, 20);

    if (count($errors) > 0) {
        foreach($errors as $error) {
            echo "<li>{$error}</li><br />";
        } 
    } else {
        echo "<p>No errors found </p>";
    }
?>

Functions are like walled gardens - you can enter and exit, but when you're inside, you can't see anyone who is outside the walls. 功能就像有围墙的花园-您可以进入和退出,但是当您进入室内时,看不到任何在墙外的人。 In order to interact with the rest of the code, you either have to pass a result back out, pass in a variable by reference, or (worst way) use a global variable. 为了与其余代码交互,您要么必须将结果传递回去,要么按引用传递变量,要么(最糟糕的方式)使用全局变量。

You could declare the $errors array as a global inside the function, and then alter it. 您可以将$ errors数组声明为函数内部的全局变量,然后对其进行更改。 This approach does not require us to return anything from the function. 这种方法不需要我们从函数返回任何东西。

function minLength($input, $min) {
    global $errors;
    if (strlen($input) <= $min) {
        //this syntax adds a new element to an array
        $errors[] = "Is that your real name? No its not.";
    } 
    //else not needed. if input is correct, do nothing...
}

You could pass in an $errors array by reference. 您可以通过引用传递$ errors数组。 This is another method that allows a globally declared variable to be altered while inside a function. 这是允许在函数内部更改全局声明的变量的另一种方法。 I'd recommend this way. 我建议这样。

function minLength($input, $min, &$errors) { //notice the &
    if (strlen($input) <= $min) {
        $errors[] = "Is that your real name? No its not.";
    } 
}
//Then the function call changes to:
minLength($test, 20, $errors); 

But for completeness, here's how you could do it with a return value. 但是为了完整起见,这是使用返回值来实现的方法。 This is tricky, since it will add a new array element whether the input is wrong or not. 这很棘手,因为无论输入是否错误,它都会添加一个新的数组元素。 We don't really want an array full of empty errors, that makes no sense. 我们真的不想要一个充满空错误的数组,这没有任何意义。 They aren't errors, so it shouldn't return anything. 它们不是错误,因此它不应返回任何内容。 To solve that we rewrite the function to return either a string or the boolean false, and we test the value when we get it back: 为了解决这个问题,我们重写了该函数以返回字符串或布尔值false,然后在返回时测试该值:

function minLength($input, $min) {
    if (strlen($input) <= $min) {
        return "Is that your real name? No it's not.";
    } else {
        return false;
    }
}

//meanwhile, in the larger script...
//we need a variable here to 'catch' the returned value of the function
$result = minLength("12345678901234", 12);
if($result){ //if it has a value other than false, add a new error
    $errors[] = $result;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM