Problems with a function

Hi everyone,

I have created a function that it´s should return an array so that I can count the length of it. But it gives me the following error:

Notice: Undefined variable: generos_sugeridos in /Applications/XAMPP/xamppfiles/htdocs/xboxone/single.php on line 275

The code I have for the function:

<?php

function contador_generos($i,$posts) {
    $generos_sugeridos = array();

    $i=0;

    if ($posts[0]['accion'] == 1){
        $generos_sugeridos[$i]= 'Acción';
        $i++;
    }

    if ($posts[0]['aventura'] == 1){
        $generos_sugeridos[$i]= 'Aventura';
        $i++;
    }

    if ($posts[0]['shooter'] == 1){
        $generos_sugeridos[$i]= 'Shooter';
        $i++;
    }

    if ($posts[0]['casual'] == 1){
        $generos_sugeridos[$i]= 'Casual';
        $i++;
    }

    if ($posts[0]['conduccion'] == 1){
        $generos_sugeridos[$i]= 'Conducción';
        $i++;
    }

    if ($posts[0]['deportes'] == 1){
        $generos_sugeridos[$i]= 'Deportes';
        $i++;
    }

    if ($posts[0]['estrategia'] == 1){
        $generos_sugeridos[$i]= 'Estrategia';
        $i++;
    }

    if ($posts[0]['MMO'] == 1){
        $generos_sugeridos[$i]= 'MMO';
        $i++;
    }
    if ($posts[0]['Rol'] == 1){
        $generos_sugeridos[$i]= 'Rol';
        $i++;
    }

    if ($posts[0]['Simulacion'] == 1){
        $generos_sugeridos[$i]= 'Simulación';
        $i++;
    }

    if ($posts[0]['terror'] == 1){
        $generos_sugeridos[$i]= 'Terror';
        $i++;
    }

    if ($posts[0]['Supervivencia'] == 1){
        $generos_sugeridos[$i]= 'Supervivencia';
        $i++;
    }

    if ($posts[0]['plataformas'] == 1){
        $generos_sugeridos[$i]= 'Plataformas';
        $i++;
    }

    if ($posts[0]['lucha'] == 1){
        $generos_sugeridos[$i]= 'Lucha';
        $i++;
    }

    if ($posts[0]['indie'] == 1){
        $generos_sugeridos[$i]= 'Indie';
        $i++;
    }

    if ($posts[0]['sandbox'] == 1){
        $generos_sugeridos[$i]= 'Sandbox';
        $i++;
    }

    if ($posts[0]['puzle'] == 1){
        $generos_sugeridos[$i]= 'Puzle';
        $i++;
    }
    if ($posts[0]['retro_antiguo'] == 1){
        $generos_sugeridos[$i]= 'Retro';
        $i++;
    }
    if ($posts[0]['arcade'] == 1){
        $generos_sugeridos[$i]= 'Arcade';
        $i++;
    }
    if ($posts[0]['gratis'] == 1){
        $generos_sugeridos[$i]= 'Free to Play';
        $i++;
    }
    if ($posts[0]['consumible'] == 1){
        $generos_sugeridos[$i]= 'Consumible';
        $i++;
    }

    return $generos_sugeridos;

}


?>

And then in another file:

for ($q=0; $q < count($sugeridos); $q++) {

            contador_generos($q,$posts);
            echo count($generos_sugeridos); //THIS IS THE LINE 275 IN MY CODE.

        }

Thanks in advice!

You are calling the function.
And the function returns the array.

But what is it returning it to?

Thanks for your response,

I haven´t understood well the question, you mean what is the content of the returning array?

I have tried using:

var_dump($generos_sugeridos)

But it gives me the same error. I´m not used to use functions.

The problem is that you create the array inside the function, then you return it from the function, but you don’t do anything to capture the array that you returned. This line here:

contador_generos($q,$posts);

should be more like

$my_array = contador_generos($q,$posts);
echo count($my_array);

Have a read up on ‘variable scope’, that might clarify things a bit. But basically when you return a variable from a function, whatever it is, you need to capture that return in the way I’ve shown above. Then you can use it in your calling code. The other way would be to create the array in the main code and declare it as a global in the function, but that’s not a nice way of coding in my opinion.

You could simplify it a bit by losing the references to $i and just use the auto-add method of adding to an array:

    if ($posts[0]['accion'] == 1){
        $generos_sugeridos[]= 'Acción';
    }

Changing all the if() clauses to work that way would produce the same array at the end.

2 Likes

Thanks!!!

It worked!!!

In fact, even in your initial implementation, there is no purpose to have the first argument of the function at all, as $q was being passed to the function as $i, however it was immediately reset to 0 at the start of the function. Removing $i altogether from the function (including as the first argument) would be advised.

function contador_generos($posts) { $generos_sugeridos = array(); ... } ... $generos_sugeridos = contador_generos($posts); echo count($generos_sugeridos);

You may be able to clean up the long list of if statements you have kicking around in that function as well, which may clean up your code a bit, though it’s certainly readable as is I suppose. It may make it more difficult to add additional fields in the future, depending on how often this type of function is used in your code.

Jeff

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.