phpfunction-call

Fatal error: Uncaught ArgumentCountError: Too few arguments to function translate(), 1 passed in and exactly 3 expected


This basic function call works;

function translate($google) {
    $en = array("Mother", "Father");
    $de = array("Mutter", "Vater");
    $s = str_replace($en, $de, $google);
    return $s;
}

But this does not work

$en = array("Mother", "Father");
$de = array("Mutter", "Vater");
function translate($google, $en, $de) {
    $s = str_replace($en, $de, $google);
    return $s;
}

When called like this:

echo translate(
         fonkfonk(
             str_replace(
                 array("\n", "\r", "\t"),
                 array("‌​", "", ""),
                 file_get_co‌​ntents($cache)
             )
         )
     );

I get:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function translate(), 1 passed in and exactly 3 expected


Solution

  • Your problem is that you're not providing the values of $en and $de to your function when calling it.

    $en = array("Mother", "Father");
    $de = array("Mutter", "Vater");
    function translate($google, $en, $de) {
        $s = str_replace($en,$de,$google);
        return $s;
    }
    
    echo translate(fonkfonk(.....)); // error because translate() does not know what
                                     // $en and $de are supposed to be
    

    You are only providing the result of the fonkfonk() function as the first argument ($google) and not providing a second and third argument.

    What you should do is either provide the values of $en and $de in your function call, or import them when you define the function:

    function translate($google, $en, $de) {
        $s = str_replace($en,$de,$google);
        return $s;
    }
    
    $en = array("Mother", "Father");
    $de = array("Mutter", "Vater");
    echo translate(fonkfonk(.....), $en, $de);
    

    Or:

    $en = array("Mother", "Father");
    $de = array("Mutter", "Vater");
    function translate($google) use ($en, $de) {
        $s = str_replace($en,$de,$google);
        return $s;
    }
    
    echo translate(fonkfonk(.....));