Error in anonymity function does not allow compiling

Asked

Viewed 55 times

0

I am studying a PHP OO book. There is an example of anonymous function, but is giving error:

# FUNÇÂO ANONIMA
$remove_acento = function($str) {
$a = array(
    'à', 'á', 'â', 'ã', 'ä', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó',
    'ô', 'õ', 'ö', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Ç', 'È', 'É',
    'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ù', 'Ú', 'Û', 'Ü', 'Ý'
);
$b = array(
    'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o',
    'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'A', 'A', 'A', 'A', 'C', 'E', 'E',
    'E', 'E', 'I', 'I', 'I', 'I', 'N', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y'
);
return str_replace($a, $b, $str);
}

print $remove_acento('José da Conceição');

When running, the following error occurs:

Parse error: syntax error, Unexpected 'print' (T_PRINT)

I see no direct error in this code. I use PHP 7.0.8.

  • 2

    I find it curious an OOP book teaching to use anonymous function :D I say that people use the term only for marketing...

  • 1

    Missing one ; before the print: $remove_acento = function($str) { };.

  • @Bigown Out of curiosity, you gave an answer and then closed the question as a typo. Is there any reason why this situation occurred?

  • 1

    @stderr Because it is just a typo, you need an answer to be finalized, but it will not meet anyone else (formerly closed as Too specific). The standard is to do this, the reason for closure exists precisely for these cases.

1 answer

4


We’re missing a ; at the end of the variable declaration:

$remove_acento = function($str) {
    $a = array(
        'à', 'á', 'â', 'ã', 'ä', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó',
        'ô', 'õ', 'ö', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Ç', 'È', 'É',
        'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ù', 'Ú', 'Û', 'Ü', 'Ý'
    );
    $b = array(
        'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o',
        'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'A', 'A', 'A', 'A', 'C', 'E', 'E',
        'E', 'E', 'I', 'I', 'I', 'I', 'N', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y'
    );
    return str_replace($a, $b, $str);
};

print $remove_acento('José da Conceição');

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.