true giving problem(bug) in code

Asked

Viewed 62 times

1

Good evening guys, I was developing a script in php and when I put true being passed by parameter to a function inside an array dynamically , he gave a bug. I can not explain the bug right, but the function works almost equal to array_unique. if placed true he acts like the array_unique, if false he simply repeats what was passed to him.

Follow the code below:

function random_caractere_v2($veredito) {
                 if($veredito) {
                    $elemento = func_get_args();
                    $array = array();
                    foreach ($elemento as $indice => $valor) {
                        if(in_array($valor, $array)) {
                            continue;
                        }
                        if(!in_array("$valor", $array)) {
                            $array[] = $valor;

                        }
                    }
                    print_r($array);
                 }
                 if(!$veredito) {
                     $elemento = func_get_args();
                     print_r($elemento);
                 }
              }


random_caractere_v2(true,'lapiseira','colchao','caderno','quimica','quimica');

I was able to solve the problem using unset to take out the problem Indice, but I’m curious to find out why the bug.

  • And what’s the bug?

  • the bug is that it only passes the value 1 that would be true 1. the rest of the values n passes , the condition is that if the value is already in the array array array it jumps the assignment and goes to the low condition that is : if the value was not found in the array array then assign it, but the n code does so , gets stuck in the first condition , as if the value was already inside the vector

  • really very strange.. switching ! in_array to ! isset here worked normal, I’ll give a search to understand the reason for this error

  • I was only able to solve , using the unset function in the vector Indice 0

  • 2

    For me it is not clear what the function is supposed to do. It was preferable to give one or two examples of input and output values, to realize the purpose of the code and then go to solve the problem.

  • with the true it will return [0] => pencil holder [1] =>square [2] =>notebook [3] => chemical

  • with false it returns the same thing that was passed , without taking the repeated value

Show 2 more comments

1 answer

1


The problem is in the function func_get_args. It takes all arguments passed to its function, including the initial boolean. You have to remove this boolean from your array and then work alone with the new elements.

function random_caractere_v2($veredito) {
    if ($veredito) {
        $elemento = func_get_args();
        // remove o primeiro elemento
        array_shift($elemento);

        $array = array();
        foreach ($elemento as $indice => $valor) {
            if(in_array($valor, $array)) {
                continue;
            }

            $array[] = $valor;
        }

        print_r($array);
    }

    if (!$veredito) {
        $elemento = func_get_args();
        // remove o primeiro elemento
        array_shift($elemento);
        print_r($elemento);
    }
}

But, another way to do it is, from PHP 5.6, use the operator ..., where you can set fixed arguments and then what is variable. For example:

function random_caractere_v2($veredito, ...$elements) {
    if ($veredito) {
        $array = array();
        foreach ($elements as $idx => $value) {
            if (in_array($value, $array)) {
                continue;
            }

            $array[] = $value;
        }

        return $array;
    }

    return $elements;
}

That will do the same thing that the other function does, but in a more elegant way.

  • type , I had decided to delete the boleano value , but n knew this operator ... but I have a doubt , I will have to pass the other values as an array ?

  • No, you pass the values as arguments, just like in the example you passed. PHP turns to turn them into an array.

  • guy had never heard of this operator ... , where can I find something about him ? , google search , but n find anything

  • In the PHP documentation you have about variable Arg list

  • vlw , much faster than func_get_args kkkkkk

Browser other questions tagged

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