简体   繁体   中英

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. 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") ?>

Then I create the array <?php $errors = array(); ?> <?php $errors = array(); ?>

Then I call the function from includes <?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. However, you don't have $errors accepting return from that function in your code.

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. 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. 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:

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;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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